简体   繁体   English

在当前文件中按下按钮后,如何打开另一个 qt 设计器文件?

[英]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_ ())

first page file second page file第一页文件第二页文件

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.

相关问题 使用QT Designer和PyQt,如何用另一个.ui文件中定义的窗口小部件填充一个.ui文件中定义的空窗口小部件? - Using QT Designer and PyQt, how do I fill an empty widget defined in one .ui file with a widget defined in another .ui file? 如何引用我在 Qt Designer 中创建的按钮? - How do I reference a button that I created in Qt Designer? 如何创建在Qt Designer中打开QFileDialog的信号? - How do I create a signal to open a QFileDialog in Qt Designer? 如何通过单击Qt Designer中的按钮来打印日期? - How do I print the date by clicking a button in Qt Designer? 如何在按下按钮时提交对文本文件的更改? - How do I commit changes to a text file on button press? 如何使用 tkinter 打开文件并将数据保存到另一个变量? - How do I open a file with tkinter and save the data to another variable? 如何使按钮按下,先停止播放音频文件,然后播放其自己的音频? - How do I make button press first stopping a playing audio file and then playing its own audio? 如何在Python中按下按钮时将标签的文本更改为文件名? - How do I change a label's text to a file name on button press in Python? 我如何访问我使用 Qt Designer 创建的项目? - How do i Acess Items that i created with Qt Designer? 使用C ++,如何在Qt Designer中运行python文件? - Using C++, how can I run a python file in Qt Designer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM