[英]How do I open another qt designer file after I press a button in the current file?
I created a simple file called (first page) through a qt designer program, and put a button named (login) in it, and I also created another simple file with the same program called (second page)我通过 qt 设计器程序创建了一个名为(第一页)的简单文件,并在其中放置了一个名为(登录)的按钮,我还使用相同的程序创建了另一个名为(第二页)的简单文件
After running the first file and pressing the button in it, I want to open the second file, or in other words I want to link the pages together.运行第一个文件并按下其中的按钮后,我想打开第二个文件,或者换句话说,我想将页面链接在一起。 How can I do this?.
我怎样才能做到这一点?。 Please i need help.
请我需要帮助。
The first file code:第一个文件代码:
from PyQt5.QtWidgets import QDialog, QApplication from PyQt5 import QtCore, QtGui, QtWidgets,uic class Ui_MainWindow(object):从 PyQt5.QtWidgets 导入 QDialog,QApplication 从 PyQt5 导入 QtCore,QtGui,QtWidgets,uic class Ui_MainWindow(对象)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 140, 91, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(12)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Login"))
self.pushButton.clicked.connect(self.buttonClicked)
def buttonClicked(self,Ui):
uic.loadUi('secondpage.ui',self)
if name == " main ": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) if name == " main ": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_ ())
I know what you mean.我知道你的意思。 So, there are solution:
所以,有解决办法:
from PyQt5.QtWidgets import QDialog, QApplication from PyQt5 import QtCore, QtGui, QtWidgets,uic
# Add second window import here
from second_file import Second_Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 140, 91, 31))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(12)
font.setBold(True)
font.setItalic(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
# Connect button with function
self.pushButton,clicked.connect(lambda x: self.buttonClicked(MainWindow))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Login"))
self.pushButton.clicked.connect(self.buttonClicked)
def buttonClicked(self, MainWindow):
SecondScreen_MW = QtWidgets.QMainWindow()
ui = Second_Ui_MainWindow()
ui.setupUi(SecondScreen_MW )
SecondScreen_MW.show()
# (Optional) Hide first screen
MainWindow.hide()
But, as said @musicamante: "You should not try to edit pyuic files, but instead use them as explained in using Designer. Also, even when using the proper way to load the UI, you should not use loadUi on an already set widget."但是,正如@musicamante 所说:“您不应尝试编辑 pyuic 文件,而应按照使用 Designer 中的说明使用它们。此外,即使使用正确的方式加载 UI,也不应在已设置的小部件上使用 loadUi 。”
This method works, but it is not the best.这种方法有效,但不是最好的。 If you need a consultation, you can contact me
需要咨询的可以联系我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.