简体   繁体   中英

PyQt opening a new Windowed Widget from a MainWindow generated from QtDesigner

Here's the situation:

  • I have one MainWindow and one Widget
  • MainWindow and Widget were created from QtDesigner.
  • The Mainwindow has one button that should open up the Widget.
  • I passed the *.ui files through pyuic in order to get them into a more pythonic form.
  • I then subclassed MainWindow so I could add a trigger to the button to call my Widget

The end goal is to have the Widget appear in a new window when I click the button in MainWindow, however the windows close with an exit code -1

Here's what the code looks like. I slightly altered ExampleWidget.py by adding a main() function.

# MainWindow.py

from PyQt5 import QtCore, QtGui, QtWidgets
from ui.ExampleWidget import main
from ui.ExampleMainWindow import Ui_MainWindow

class Ui_MainWindow(Ui_MainWindow):
    def setupUi(self, MainWindow):
        super().setupUi(MainWindow)
        self.pushButton.clicked.connect(main)


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

# ExampleWidget.py

from PyQt5 import QtCore, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(Form)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(("Form"))
        self.label.setText(("It worked"))


def main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

Your code has several errors, it is not correct that the class you inherit has the same name as the parent. Another error is that there can only be one QApplication since it creates a loop where the GUI lives, and when you press the button and call main() , you create another loop that is blocking the initial loop.

It is advisable not to use the class generated by Qt Designer since it is not a widget and we can not handle certain events, it is appropriate to implement a widget that uses the design.

according to your code I can assume that the classes generated by Qt Designer are Ui_MainWindow and Ui_Form that are in the files ExampleMainWindow and ExampleWidget, respectively, and both within the ui folder. Then you must implement the classes in the following way:

from PyQt5 import QtCore, QtGui, QtWidgets
from ui.ExampleWidget import Ui_Form
from ui.ExampleMainWindow import Ui_MainWindow

class Form(QtWidgets.QWidget, Ui_Form):
    def __init__(self, *args, **kwargs):
        QtWidgets.QWidget.__init__(self, *args, **kwargs)
        self.setupUi(self)

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.onClicked)

    def onClicked(self):
        self.form = Form()
        self.form.show()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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