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