【问题标题】:Pop up Window or Multiple Windows with PyQt5/QtDesigner [duplicate]使用 PyQt5/QtDesigner 弹出窗口或多个窗口 [重复]
【发布时间】:2019-06-02 12:21:50
【问题描述】:

我已经学习 python 1 个月了。我可以为我的 ui 创建一个弹出窗口吗?我想在 MainWindow 上按一个按钮并打开一个新窗口。

最好的问候。

【问题讨论】:

  • 无法弄清楚 uic 模块的作用,但我会调查一下,谢谢。我使用 pyuic5 xx.ui -o xx.py 进行转换。

标签: python python-3.x pyqt5 qt-designer


【解决方案1】:

试试看:

main.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *

from mainwindow_ui import Ui_MainWindow
from dialog_ui     import Ui_Dialog

class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        gridLayout = QGridLayout(centralWidget)
        gridLayout.addWidget(self.ui.label,      0, 0, alignment=Qt.AlignCenter)
        gridLayout.addWidget(self.ui.pushButton, 1, 0)

    def dialogbox(self):
        self.hide()
        self.myDialog = MyDialog()
        self.myDialog.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

mainwindow_ui.py

from PyQt5 import QtCore, QtGui, QtWidgets
from dialog_ui import Ui_Dialog

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(685, 353)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(240, 66, 331, 181))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 300, 200, 27))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 685, 25))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(MainWindow.dialogbox) 

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

        self.label.setText(_translate("MainWindow", 
            """
                <p><span style=\" font-size:26pt;\">MainWindow</span></p>
                <hr> 
            """))
        self.pushButton.setText(_translate("MainWindow", "Open a new window"))

dialog_ui.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 211)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(20, 70, 360, 71))
        self.label.setObjectName("label")

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle("Dialog")
        self.label.setText(
            """
              <p><br><br><span style=\" font-size:28pt;\">This is a new window.</span></p>
            """)

【讨论】:

  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-08-10
  • 2020-07-01
  • 1970-01-01
  • 2019-09-14
  • 2014-06-05
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多