Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using multiple Windows PyQt5



  • Hello Guys,
    I'm working hard on my project.
    I've got some issues...I'm trying to open 3 windows via push buttons.
    It is no problem to change from Window 1 to Window 2, but it's not possible to open the third window via push botton. But it is possible to start with the second window and switch via push button to the third window.
    Maybe someone can help me.

    erstesFenster.py :

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'ersteFenster.ui'
    #
    # Created by: PyQt5 UI code generator 5.11.3
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QTimer, QDate, Qt
    import time
    
    
    class Ui_StartFenster(object):
    
        def openWindow2(self):
            from zweitesFenster import Ui_Rotortypauswaehlen
            self.window2 = QtWidgets.QDialog()
            self.ui = Ui_Rotortypauswaehlen()
            self.ui.setupUi(self.window2)
            StartFenster.hide()
            self.window2.show()
    
        def setupUi(self, StartFenster):
            StartFenster.setObjectName("StartFenster")
            StartFenster.resize(790, 435)
            self.centralwidget = QtWidgets.QWidget(StartFenster)
            self.centralwidget.setObjectName("centralwidget")
    
            self.buttonstart = QtWidgets.QPushButton(self.centralwidget)
            self.buttonstart.setGeometry(QtCore.QRect(240, 300, 99, 30))
            self.buttonstart.setObjectName("buttonstart")
            self.buttonstart.clicked.connect(self.openWindow2)
    
            self.buttonabbruch = QtWidgets.QPushButton(self.centralwidget)
            self.buttonabbruch.setGeometry(QtCore.QRect(400, 300, 99, 30))
            self.buttonabbruch.setObjectName("buttonabbruch")
    
            self.label_xfenster = QtWidgets.QLabel(self.centralwidget)
            self.label_xfenster.setGeometry(QtCore.QRect(10, -20, 731, 331))
            self.label_xfenster.setStyleSheet("image: url(:/newPrefix/Download.png);")
            self.label_xfenster.setObjectName("label_xfenster")
    
            self.label_copyrights = QtWidgets.QLabel(self.centralwidget)
            self.label_copyrights.setGeometry(QtCore.QRect(10, 350, 171, 22))
            self.label_copyrights.setTextFormat(QtCore.Qt.RichText)
            self.label_copyrights.setObjectName("label_copyrights")
    
            self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.centralwidget)
            self.dateTimeEdit.setGeometry(QtCore.QRect(600, 350, 194, 32))
            self.dateTimeEdit.setObjectName("dateTimeEdit")
    
            StartFenster.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(StartFenster)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 790, 28))
            self.menubar.setObjectName("menubar")
            StartFenster.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(StartFenster)
            self.statusbar.setObjectName("statusbar")
            StartFenster.setStatusBar(self.statusbar)
    
            self.retranslateUi(StartFenster)
            QtCore.QMetaObject.connectSlotsByName(StartFenster)
    
        def retranslateUi(self, StartFenster):
            _translate = QtCore.QCoreApplication.translate
            StartFenster.setWindowTitle(_translate("StartFenster", "Rotor-Heizprozess"))
            self.buttonstart.setText(_translate("StartFenster", "Los geht\'s!"))
            self.buttonabbruch.setText(_translate("StartFenster", "Abbrechen"))
            self.label_siemensfenster.setText(_translate("StartFenster", "<html><head/><body><p><br/></p></body></html>"))
            self.label_copyrights.setText(_translate("StartFenster", "<html><head/><body><p><span style=\" font-size:10pt; font-style:italic; text-decoration: underline;\">Copyright Horstmann, Weidner</span></p></body></html>"))
    
    import source
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        StartFenster = QtWidgets.QMainWindow()
        ui = Ui_StartFenster()
        ui.setupUi(StartFenster)
        StartFenster.show()
        sys.exit(app.exec_())
    
    

    zweitesFenster.py :

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'zweitesFenster.ui'
    #
    # Created by: PyQt5 UI code generator 5.11.3
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_Rotortypauswaehlen(object):
    
        def openWindow3(self):
            from drittesFenster import Ui_Dialog
            self.window3 = QtWidgets.QDialog()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self.window3)
            Rotortypauswaehlen.hide()
            self.window3.show()
    
        def setupUi(self, Rotortypauswaehlen):
            Rotortypauswaehlen.setObjectName("Rotortypauswaehlen")
            Rotortypauswaehlen.resize(790, 435)
    
            self.label = QtWidgets.QLabel(Rotortypauswaehlen)
            self.label.setGeometry(QtCore.QRect(10, 210, 211, 22))
            self.label.setObjectName("label")
            self.label_copyrights = QtWidgets.QLabel(Rotortypauswaehlen)
            self.label_copyrights.setGeometry(QtCore.QRect(10, 400, 171, 22))
            self.label_copyrights.setTextFormat(QtCore.Qt.RichText)
            self.label_copyrights.setObjectName("label_copyrights")
    
            self.start = QtWidgets.QPushButton(Rotortypauswaehlen)
            self.start.setGeometry(QtCore.QRect(231, 331, 99, 30))
            self.start.setObjectName("start")
            self.start.clicked.connect(self.openWindow3)
    
            self.abbrechen = QtWidgets.QPushButton(Rotortypauswaehlen)
            self.abbrechen.setGeometry(QtCore.QRect(380, 330, 99, 30))
            self.abbrechen.setObjectName("abbrechen")
    
            self.dateTimeEdit = QtWidgets.QDateTimeEdit(Rotortypauswaehlen)
            self.dateTimeEdit.setGeometry(QtCore.QRect(580, 390, 194, 32))
            self.dateTimeEdit.setObjectName("dateTimeEdit")
    
            self.widget = QtWidgets.QWidget(Rotortypauswaehlen)
            self.widget.setGeometry(QtCore.QRect(230, 140, 143, 161))
            self.widget.setObjectName("widget")
    
            self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setObjectName("verticalLayout")
    
            self.radioButton = QtWidgets.QRadioButton(self.widget)
            self.radioButton.setObjectName("radioButton")
            self.verticalLayout.addWidget(self.radioButton)
            self.radioButton_2 = QtWidgets.QRadioButton(self.widget)
            self.radioButton_2.setObjectName("radioButton_2")
            self.verticalLayout.addWidget(self.radioButton_2)
            self.radioButton_3 = QtWidgets.QRadioButton(self.widget)
            self.radioButton_3.setObjectName("radioButton_3")
            self.verticalLayout.addWidget(self.radioButton_3)
            self.radioButton_4 = QtWidgets.QRadioButton(self.widget)
            self.radioButton_4.setObjectName("radioButton_4")
            self.verticalLayout.addWidget(self.radioButton_4)
            self.radioButton_5 = QtWidgets.QRadioButton(self.widget)
            self.radioButton_5.setObjectName("radioButton_5")
            self.verticalLayout.addWidget(self.radioButton_5)
    
            self.retranslateUi(Rotortypauswaehlen)
            QtCore.QMetaObject.connectSlotsByName(Rotortypauswaehlen)
    
        def retranslateUi(self, Rotortypauswaehlen):
            _translate = QtCore.QCoreApplication.translate
            Rotortypauswaehlen.setWindowTitle(_translate("Rotortypauswaehlen", "Rotor-Heizprozess"))
            self.label.setText(_translate("Rotortypauswaehlen", "<html><head/><body><p align=\"center\"><span style=\" font-weight:600; text-decoration: underline;\">Bitte Rotortyp auswählen:</span></p></body></html>"))
            self.label_copyrights.setText(_translate("Rotortypauswaehlen", "<html><head/><body><p><span style=\" font-size:10pt; font-style:italic;\">Copyright Horstmann, Weidner</span></p></body></html>"))
            self.start.setText(_translate("Rotortypauswaehlen", "Start!"))
            self.abbrechen.setText(_translate("Rotortypauswaehlen", "Abbruch!"))
            self.radioButton.setText(_translate("Rotortypauswaehlen", "S4R3"))
            self.radioButton_2.setText(_translate("Rotortypauswaehlen", "TLRI 115-36"))
            self.radioButton_3.setText(_translate("Rotortypauswaehlen", "S4R4"))
            self.radioButton_4.setText(_translate("Rotortypauswaehlen", "Essenernudeln"))
            self.radioButton_5.setText(_translate("Rotortypauswaehlen", "heutefreisaufen"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Rotortypauswaehlen = QtWidgets.QDialog()
        ui = Ui_Rotortypauswaehlen()
        ui.setupUi(Rotortypauswaehlen)
        Rotortypauswaehlen.show()
        sys.exit(app.exec_())
    
    

    drittesFenster.py :

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'drittesFenster.ui'
    #
    # Created by: PyQt5 UI code generator 5.11.3
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    from Tempauslesen_Test import Temperatur1
    
    
    
    class Ui_Dialog(object):
    
        def openWindow4(self):
            from viertesFenster import Ui_Dialog
            self.window4 = QtWidgets.QDialog()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self.window4)
            Dialog.hide()
            self.window4.show()
    
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(790, 435)
            self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
            self.lcdNumber.setGeometry(QtCore.QRect(620, 50, 64, 23))
            self.lcdNumber.setObjectName("lcdNumber")
            self.lcdNumber_2 = QtWidgets.QLCDNumber(Dialog)
            self.lcdNumber_2.setGeometry(QtCore.QRect(620, 90, 64, 23))
            self.lcdNumber_2.setObjectName("lcdNumber_2")
            self.label_9 = QtWidgets.QLabel(Dialog)
            self.label_9.setGeometry(QtCore.QRect(520, 50, 68, 22))
            self.label_9.setObjectName("label_9")
            self.label_10 = QtWidgets.QLabel(Dialog)
            self.label_10.setGeometry(QtCore.QRect(520, 90, 71, 22))
            self.label_10.setObjectName("label_10")
            self.timeEdit = QtWidgets.QTimeEdit(Dialog)
            self.timeEdit.setGeometry(QtCore.QRect(620, 130, 118, 32))
            self.timeEdit.setObjectName("timeEdit")
            self.label_11 = QtWidgets.QLabel(Dialog)
            self.label_11.setGeometry(QtCore.QRect(520, 140, 68, 22))
            self.label_11.setObjectName("label_11")
            self.pushButton = QtWidgets.QPushButton(Dialog)
            self.pushButton.setGeometry(QtCore.QRect(540, 220, 171, 131))
            self.pushButton.setObjectName("pushButton")
            self.pushButton.clicked.connect(self.openWindow4)
            self.label_copyrights = QtWidgets.QLabel(Dialog)
            self.label_copyrights.setGeometry(QtCore.QRect(620, 400, 171, 22))
            self.label_copyrights.setTextFormat(QtCore.Qt.RichText)
            self.label_copyrights.setObjectName("label_copyrights")
            self.widget = QtWidgets.QWidget(Dialog)
            self.widget.setGeometry(QtCore.QRect(100, 10, 361, 421))
            self.widget.setObjectName("widget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setObjectName("verticalLayout")
    
            self.progressBar_1 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_1.setProperty("value", 24)
            self.progressBar_1.setObjectName("progressBar_1")
            self.progressBar_1.setMaximum(300)
            self.verticalLayout.addWidget(self.progressBar_1)
    
            self.progressBar_2 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_2.setProperty("value", 24)
            self.progressBar_2.setObjectName("progressBar_2")
            self.verticalLayout.addWidget(self.progressBar_2)
    
            self.progressBar_3 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_3.setProperty("value", 24)
            self.progressBar_3.setObjectName("progressBar_3")
            self.verticalLayout.addWidget(self.progressBar_3)
            
            self.progressBar_4 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_4.setProperty("value", 24)
            self.progressBar_4.setObjectName("progressBar_4")
            self.verticalLayout.addWidget(self.progressBar_4)
            
            self.progressBar_5 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_5.setProperty("value", 24)
            self.progressBar_5.setObjectName("progressBar_5")
            self.verticalLayout.addWidget(self.progressBar_5)
            self.progressBar_6 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_6.setProperty("value", 24)
            self.progressBar_6.setObjectName("progressBar_6")
            self.verticalLayout.addWidget(self.progressBar_6)
            self.progressBar_7 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_7.setProperty("value", 24)
            self.progressBar_7.setObjectName("progressBar_7")
            self.verticalLayout.addWidget(self.progressBar_7)
            self.progressBar_8 = QtWidgets.QProgressBar(self.widget)
            self.progressBar_8.setProperty("value", 24)
            self.progressBar_8.setObjectName("progressBar_8")
            self.verticalLayout.addWidget(self.progressBar_8)
            self.widget1 = QtWidgets.QWidget(Dialog)
            self.widget1.setGeometry(QtCore.QRect(10, 20, 70, 401))
            self.widget1.setObjectName("widget1")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget1)
            self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.label_1 = QtWidgets.QLabel(self.widget1)
            self.label_1.setObjectName("label_1")
            self.verticalLayout_2.addWidget(self.label_1)
            self.label_2 = QtWidgets.QLabel(self.widget1)
            self.label_2.setObjectName("label_2")
            self.verticalLayout_2.addWidget(self.label_2)
            self.label_3 = QtWidgets.QLabel(self.widget1)
            self.label_3.setObjectName("label_3")
            self.verticalLayout_2.addWidget(self.label_3)
            self.label_4 = QtWidgets.QLabel(self.widget1)
            self.label_4.setObjectName("label_4")
            self.verticalLayout_2.addWidget(self.label_4)
            self.label_5 = QtWidgets.QLabel(self.widget1)
            self.label_5.setObjectName("label_5")
            self.verticalLayout_2.addWidget(self.label_5)
            self.label_6 = QtWidgets.QLabel(self.widget1)
            self.label_6.setObjectName("label_6")
            self.verticalLayout_2.addWidget(self.label_6)
            self.label_7 = QtWidgets.QLabel(self.widget1)
            self.label_7.setObjectName("label_7")
            self.verticalLayout_2.addWidget(self.label_7)
            self.label_8 = QtWidgets.QLabel(self.widget1)
            self.label_8.setObjectName("label_8")
            self.verticalLayout_2.addWidget(self.label_8)
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
            self.timer = QTimer()
            self.timer.timeout.connect(self.update_temperature)
            self.timer.start(1)
    
        def update_temperature(self):
            temperature = Temperatur1()
            if temperature < 500:
                self.progressBar_1.setValue(temperature)
            else:
                self.timer.stop()
    
        def retranslateUi(self, Dialog):
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Rotor-Heizprozess"))
            self.label_9.setText(_translate("Dialog", "Strom"))
            self.label_10.setText(_translate("Dialog", "Spannung"))
            self.label_11.setText(_translate("Dialog", "Laufzeit:"))
            self.pushButton.setText(_translate("Dialog", "Abbruch!!"))
            self.label_copyrights.setText(_translate("Dialog", "<html><head/><body><p><span style=\" font-size:10pt; font-style:italic;\">Copyright Horstmann, Weidner</span></p></body></html>"))
            self.progressBar_1.setWhatsThis(_translate("Dialog", "<html><head/><body><p><br/></p></body></html>"))
            self.label_1.setText(_translate("Dialog", "Thermo1"))
            self.label_2.setText(_translate("Dialog", "Thermo2"))
            self.label_3.setText(_translate("Dialog", "Thermo3"))
            self.label_4.setText(_translate("Dialog", "Thermo4"))
            self.label_5.setText(_translate("Dialog", "Thermo5"))
            self.label_6.setText(_translate("Dialog", "Thermo6"))
            self.label_7.setText(_translate("Dialog", "Thermo7"))
            self.label_8.setText(_translate("Dialog", "Thermo8"))
            self.progressBar_1.setFormat(_translate("MainWindow", '%v°C'))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    
    
    

    Traceback (most recent call last):
    File "/home/pi/Desktop/Testphase/neueGui2in1/zweitesFenster.py", line 19, in openWindow3
    Rotortypauswaehlen.close()
    NameError: global name 'Rotortypauswaehlen' is not defined
    Aborted

    This is the error, after clicking on pushbutton in window 2



  • @Starstriker
    That seems like an awful lot of code to post for a question.


  • Lifetime Qt Champion

    @Starstriker

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_Rotortypauswaehlen(object):
    
        def openWindow3(self):
            from drittesFenster import Ui_Dialog
            self.window3 = QtWidgets.QDialog()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self.window3)
            Rotortypauswaehlen.hide()
            self.window3.show()
    

    Rotortypauswaehlen does not exist inside openWindow3, so how is it supposed to work?
    You would need to store it in a member variable when it is passed to setupUi and then use that variable.

    Just a note: your naming convention is irritating. Usually parameter names start with lower case letter and class names with upper case letter.


  • Banned

    @jsulm the OPs naming conventions are just that conventions (aka style) -- you must not have worked with much post production code done by numerous programmers that had no official standard to follow -- as such the OPs naming conventions are just fine in that regards you just need to learn to adapt better



  • @jsulm , @Starstriker
    Just wanted to say that, contrary to what @Denni-0 has posted, the naming conventions suggested by @jsulm would indeed be a good improvement in your coding style, and are recognised by most programmers. I too found your code difficult to absorb because of the capitalization. It is not true that @jsulm "just need to learn to adapt better", nor would I suggest that he "must not have worked with much post production code".

    We have had this disagreement on this forum before, so there is little point in re-rehearsing the arguments. Suffice to say that you will find a large number of programmers out there who would agree with @jsulm's recommendation.


  • Banned

    @JonB the naming convention would only be an improvement if it were actually an issue and frankly it is only an issue for the non-adaptable folk who have never worked on numerous post production code projects and it is such an infinitesimally small issue that is not even worth wasting time to even mention it but you all got to be critical about something dont you?

    As for those that agree with this -- yes there are a large number of programmers out there with virtually no experience that like to criticize others over things that are again not even worth mentioning. As such @Starstriker I would suggest doing what many others do just ignore these folk as they obviously cannot deal with reality and just seem to want to force everybody to follow their ways of thinking no matter how ridiculously minor it might be.

    Now @Starstriker getting to the meat and potatoes of the situation -- there are numerous issues within your code that go well beyond the extremely minor issue of adhering to someone else's naming conventions from what I can see you have 3 different stand alone programs but from what it sounds like you are starting (1) program and then launching the other 2 as separate windows -- is that assumption correct. Or do you have these setup as 3 separate programs because you are not sure which one the user will launch first (which you could actually dictate) and as such are then launching these other programs (not windows) via whatever program was launched first?

    Note I am kind of leaning towards the former based on the fact that you import these stand alone programs into stand alone programs which means I do not think you understand how a class works maybe -- or you have other reasons for this. You are also importing things that would be required to actually run this and test it that you have not included within the supplied code.


Log in to reply