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

时间:2014-04-04 01:17:37

标签: python window pyqt

我有2个表单,form_1和form_2(在另一个类和文件中),我将从form_1打开form_2和按钮,如何在Pyqt中创建它?

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 ??????

2 个答案:

答案 0 :(得分:0)

我仍然是pyqt的新手,但我认为你可以这样做

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

或者如果您已将其子类化,请确保将其导入,如果它在另一个文件中并使用

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

编辑 - 哦,并确保将其连接到按钮单击

self.bt_form1.clicked.connect(self.show_form2)

答案 1 :(得分:0)

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()

您的Form2代码应该类似于:

class Form2(QDialog):

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

    # do whatever #