gpt4 book ai didi

python - 使用 PyQt5/QtDesigner 弹出窗口或多个窗口

转载 作者:行者123 更新时间:2023-12-01 01:18:36 72 4
gpt4 key购买 nike

我已经学习Python 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>
""")

enter image description here

关于python - 使用 PyQt5/QtDesigner 弹出窗口或多个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54081118/

72 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com