简体   繁体   English

如何从pyqt的第一个窗口打开第二个窗口?

[英]How to open second window from 1st window in pyqt?

I have 2 forms, form_1 and form_2 (in another class and file ), I'll open form_2 from form_1 with button,我有 2 个表单, form_1form_2 (在另一个classfile ),我将使用按钮从form_1打开form_2
How to create it in PyQT?如何在 PyQT 中创建它?

form_1 code : form_1代码:

def retranslateUi(self, MainWindow):
    QtCore.QObject.connect(
        self.bt_form1, QtCore.SIGNAL(_fromUtf8("clicked()")), self.show_form2())
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    self.bt_form1.setText(_translate("MainWindow", "FORM_1", None))

def show_form2(self):
    self.form2 = form2(self) # in here ??????

I'm still pretty new to PyQT but I think you can do it like this -我对 PyQT 还是很陌生,但我认为你可以这样做 -

def show_form2(self):
    newDialog = uic.loadUi(r"uifile.ui")
    newDialog.show()

or if you've subclassed it, make sure you import it if it's in another file and use或者,如果您已将其子类化,请确保在另一个文件中import它并使用

def show_form2(self):
    newDialog = subDialog.subDialog()
    newDialog.show()

edit - oh and make sure you connect it to the buttons click编辑 - 哦,并确保将其连接到按钮单击

self.bt_form1.clicked.connect(self.show_form2)
class Main(QMainWindow):

    def __init__( ... )

    def retranslateUi(self, MainWindow):
        QtCore.QObject.connect(self.bt_form1,     QtCore.SIGNAL(_fromUtf8("clicked()")), self.show_form2())
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.bt_form1.setText(_translate("MainWindow", "FORM_1", None))

    def show_form2(self):
        self.form2 = Form2(self)
        self.form2.show()

Your Form2 code should resemble this:您的 Form2 代码应类似于:

class Form2(QDialog):

    def __init__(self, parent=None) .... 

    # do whatever #

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM