PyQt5在主窗口内打开子窗口

**2019/7/25 更正

1.使用Qt Designer中的Widget

PyQt5在主窗口内打开子窗口_第1张图片

将其拖入主窗口中,向里面加入一个Lable,然后在同样位置在放置一个同样大小的Widget,效果如下:

PyQt5在主窗口内打开子窗口_第2张图片

 

转换后代码为:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.12.3
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(350, 250)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.xianjian_widget = QtWidgets.QWidget(self.centralwidget)
        self.xianjian_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
        self.xianjian_widget.setObjectName("xianjian_widget")
        self.label = QtWidgets.QLabel(self.xianjian_widget)
        self.label.setGeometry(QtCore.QRect(70, 70, 91, 16))
        self.label.setObjectName("label")
        self.qixia_widget = QtWidgets.QWidget(self.centralwidget)
        self.qixia_widget.setGeometry(QtCore.QRect(0, 0, 300, 200))
        self.qixia_widget.setObjectName("qixia_widget")
        self.label_2 = QtWidgets.QLabel(self.qixia_widget)
        self.label_2.setGeometry(QtCore.QRect(110, 60, 54, 12))
        self.label_2.setObjectName("label_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 350, 23))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        MainWindow.setMenuBar(self.menubar)
        self.actionXian = QtWidgets.QAction(MainWindow)
        self.actionXian.setObjectName("actionXian")
        self.actionQi = QtWidgets.QAction(MainWindow)
        self.actionQi.setObjectName("actionQi")
        self.menu.addAction(self.actionXian)
        self.menu.addAction(self.actionQi)
        self.menubar.addAction(self.menu.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "仙剑窗口"))
        self.label_2.setText(_translate("MainWindow", "奇侠窗口"))
        self.menu.setTitle(_translate("MainWindow", "仙剑奇侠"))
        self.actionXian.setText(_translate("MainWindow", "Xian"))
        self.actionQi.setText(_translate("MainWindow", "Qi"))

2.创建**.py

# -*- coding: utf-8 -*- 
# Time : 2019/7/25 11:37 
# Author : hubozhi
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import QThread,Qt,QPoint
from PyQt5.QtGui import QMouseEvent
from untitled import Ui_MainWindow
import sys,redis,os

class Main(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(Main, self).__init__()
        self.setupUi(self)
        # 首先隐藏所有widget
        self.xianjian_widget.hide()
        self.qixia_widget.hide()
        self.actionXian.triggered.connect(self.openXianjian)
        self.actionQi.triggered.connect(self.openQixia)

    def openXianjian(self):
        self.qixia_widget.hide()
        self.xianjian_widget.show()

    def openQixia(self):
        self.xianjian_widget.hide()
        self.qixia_widget.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    Main = Main()
    Main.show()
    sys.exit(app.exec_())
    pass

3.在主窗口里有两个选项(“Xian”和“Qi”)与两个子窗口关联

4.“Xian”打开

PyQt5在主窗口内打开子窗口_第3张图片

 

5.“Qi”打开

PyQt5在主窗口内打开子窗口_第4张图片

 

###############################################

小技巧

发现出现了两个底边那个东西

在由窗口文件生成的.py文件中找到如下代码

        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

或者在Qt Designer设计时找到

PyQt5在主窗口内打开子窗口_第5张图片

删掉任意一个就OK了

你可能感兴趣的:(PyQt5,PyQt5)