質問

I have 2 forms, form_1 and form_2 (in another class and file), I'll open form_2 from form_1 with button,
How to create it in PyQT?

form_1 code :

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 -

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

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:

class Form2(QDialog):

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

    # do whatever #
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top