0

when the user clicks the button "PLAY" of the window1, the button calls the function "playgame" which basically invokes the class of the window2 and is added on the stacked widget , but every time I click the button, the program crashes without displaying any specific error in the terminal. Is there any solution ?

NOTE : both files are generated by qt designer, I converted them to py and I added some changes

window1.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'game_flag.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from PyQt5 import QtCore, QtWidgets
import sys
from window2 import gameart

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(889, 680)
        MainWindow.setStyleSheet("background-color : #161219;")
        MainWindow.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(260, 300, 381, 141))
        self.pushButton.setStyleSheet("QPushButton{\n"
        "    border: 4px  groove #BC006C;\n"
        "    border-radius: 20px;\n"
        "    color:white;\n"
        "    font: 20pt \"Lexend SemiBold\";\n"
        "}\n"
        "QPushButton:hover{\n"
        "    background-color : #800048;\n"
        "    \n"
        "}\n"
        "")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.playgame) #when clicked, calls the function playgame
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PLAY"))
    def playgame(self): #call the mainwindow of window2
        secondwindow = gameart().start()
        widget.addWidget(secondwindow)
        widget.setCurrentIndex(secondwindow)
class gameintro():
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())

if __name__ == "__main__":
    firstwindow = gameintro()
    widget = QtWidgets.QStackedWidget()
    widget.addWidget(firstwindow) #add the mainwindow of window1 to the stackwidget
    widget.setCurrentIndex(firstwindow)

window2.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'game_flag.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtWidgets
import sys



class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(889, 680)
        MainWindow.setStyleSheet("background-color : #161219;")
        MainWindow.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        MainWindow.setCentralWidget(self.centralwidget)
        self.centralwidget.setObjectName("centralwidget")
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(260, 300, 381, 141))
        self.pushButton.setStyleSheet("QPushButton{\n"
                                      "    border: 4px  groove #BC006C;\n"
                                      "    border-radius: 20px;\n"
                                      "    color:white;\n"
                                      "    font: 20pt \"Lexend SemiBold\";\n"
                                      "}\n"
                                      "QPushButton:hover{\n"
                                      "    background-color : #800048;\n"
                                      "    \n"
                                      "}\n"
                                      "")
        self.pushButton.setObjectName("pushButton")
        self.retranslateUi(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle("Country Game")
        self.pushButton.setText(_translate("MainWindow", "TEST"))

class gameart():
    def start(self):
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        app.exec_()
Bored Nerd
  • 64
  • 3
  • There are several issues with your code, starting with the fact that you shouldn't edit pyuic generated files (as clearly suggested by the comment header), but instead use those files **as they are** as explained in the official guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). Then, you never create the stacked widget, since `gameintro()` never returns until the application is quit. The crash is because you're trying to start another QApplication, which isn't allowed (and useless), while you should just create a new widget and set its ui. – musicamante Sep 07 '22 at 18:12
  • Also note that adding QMainWindow to a stacked widget is discouraged, mostly because it's a class intended for *top level windows*. Just use a standard QWidget ("widget" in the Designer wizard) or any other container widget like QFrame or QGroupBox. Be aware that there are a couple of tutorials on Youtube that suggest to add QMainWindow (or QDialog, which is even worse) to QStackedWidget. If your code is based on them, I strongly suggest you to *ignore* them and find other resources, as they also use *a lot* of other terrible practices. – musicamante Sep 07 '22 at 18:21
  • @musicamante firstly thank you for the advice. I know that I shouldn't modify the ui but I did it for two main reasons : a lot of videos on YT do it, and to implement some functions. Anyway, in this case it would be better to create another class that handles the logic of the ui file, grazie mille :) – Bored Nerd Sep 07 '22 at 18:30
  • I know that many Youtube videos suggest to do so, but they are ***wrong***. Just here on SO we *daily* see an average of at least 2-3 posts asking about problems that are specifically caused by that practice, which is suggested without any context and, most importantly, without considering the *consequences*: they probably played with Qt for a week (at best) or so and they made a video without any real experience. There is a reason if that warning says "unless you *know* what you are doing", and they clearly do ***not*** know that. Watch [this](//youtu.be/XXPNpdaK9WA) to better understand why. – musicamante Sep 07 '22 at 19:25
  • So, please follow the guide linked above, you will see that doing what it suggests *does* allow to implement your own functions, but it does it in the *correct* way, without ever touching the generated code. The main benefit of this is that whenever you need to change something in the UI you just have to recreate the file with pyuic without ever touching *your* code (since the class is imported), but another important reason is that those classes are *not* Qt classes (they're just python objects), so trying to override any function (i.e. `mousePressEvent()`) will have absolutely no effect. – musicamante Sep 07 '22 at 19:33
  • @musicamante Thank you very much for the video, you have basically opened my eyes about PyQt, quick question, is there a course of PyQT5 which you would recommend me to watch it? – Bored Nerd Sep 07 '22 at 20:54
  • Well, you're very welcome. Sorry, but besides the known [tutorials](https://wiki.python.org/moin/PyQt/Tutorials), I don't really have other resources I can suggest (but I'm really considering about compiling a list). I'm actually a (almost) completely self-taught programmer, meaning that I learned most of what I know *in the hard way*: trying to do things only on my own, assuming that I "did" know enough, ignoring RTFMs and documentation at first, facing my terrible mistakes along the way and painfully admitting to myself that I had to be patient with my research, reading and studying. -> – musicamante Sep 08 '22 at 05:00
  • -> I don't really regret all that, it was an extremely important aspect I probably needed to face. And it was extremely important to make me more responsible about the learning process. But if I had something/someone to "force" me to be more patient, and let me understand the responsibility of that learning, it would have probably been much more easier and less painful. So, even if I can't suggest you any other resource, the "lesson" remains: try things, push your limits, don't be too fearful, but also be *very* patient, take your time to research and *study* what others have done before you. – musicamante Sep 08 '22 at 05:07
  • @musicamante Hey dude, I wanted to tell you that thanks to that video I managed to finish the project [Guess the flag](https://github.com/BoredNerd/Guess-the-Flag). Thank you very much! – Bored Nerd Sep 16 '22 at 20:31

0 Answers0