本文介绍了使用 PyQt5/QtDesigner 弹出窗口或多个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经学习 Python 1 个月了.我可以为我的 ui 创建一个弹出窗口吗?我想按下 MainWindow 上的一个按钮并打开一个新窗口.
I've been studying python for 1 month. Can I create a pop up window for my ui? I want to press a button on MainWindow and open a new window.
最好的问候.
推荐答案
试试看:
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
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
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>
""")
这篇关于使用 PyQt5/QtDesigner 弹出窗口或多个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!