我使用pyqt5作为gui,用python制作了Mastermind游戏。程序 MainMenu.py 包含3个按钮,按下这些按钮可关闭主菜单窗口,并为各自的困难打开一个新窗口。
但是,当我按下其中一个按钮打开新窗口时,会出现错误:
Exception has occurred: SystemExit
-1
此错误发生在sys.exit(easyApp.exec_())
行。
有人知道如何解决此问题吗?
程序MainMenu.py:
from PyQt5 import QtCore, QtGui, QtWidgets # import PyQt5
import sys # import sys
# import easy, medium and hard classes
import easy
import medium
import hard
class MainMenu(): # MainMenu class to add UI for the main menu of the program:
def __init__(self, MainWindow): # __init__ method to add widgets to UI
MainWindow.setObjectName("Mastermind") # set window title
MainWindow.resize(343, 329) # resize window to be smaller
MainWindow.setStyleSheet(
"background-color: rgb(125, 201, 197);") # set bg colour
# Set central widget
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Add vertical layout widget to keep all widgets in line
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 318, 279))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
# Label to contain title
self.titleLbl = QtWidgets.QLabel(self.verticalLayoutWidget)
self.titleLbl.setStyleSheet("font: 18pt \"Kalam\";\n"
"color: rgb(224, 213, 166);\n"
"background-color: rgb(28, 31, 55);")
self.titleLbl.setObjectName("titleLbl")
self.verticalLayout.addWidget(self.titleLbl)
# Spacer to add a gap between widgets
spacerItem = QtWidgets.QSpacerItem(
20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem)
# Label to ask user to enter their name
self.usernameLbl = QtWidgets.QLabel(self.verticalLayoutWidget)
self.usernameLbl.setStyleSheet("font: 10pt \"Kalam\";\n"
"color: rgb(224, 213, 166);\n"
"background-color: rgb(28, 31, 55);")
self.usernameLbl.setObjectName("usernameLbl")
self.verticalLayout.addWidget(self.usernameLbl)
# Another spacer
spacerItem1 = QtWidgets.QSpacerItem(
20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem1)
# Input widget for user to input their name
self.nameInp = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
self.nameInp.setStyleSheet("background-color: rgb(28, 31, 55);\n"
"color: rgb(224, 213, 166);\n"
"font: 8pt \"Kalam\";")
self.nameInp.setObjectName("nameInp")
self.verticalLayout.addWidget(self.nameInp)
# Another spacer
spacerItem2 = QtWidgets.QSpacerItem(
20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem2)
# Label to ask user to select a difficulty
self.difficultyLbl = QtWidgets.QLabel(self.verticalLayoutWidget)
self.difficultyLbl.setStyleSheet("font: 10pt \"Kalam\";\n"
"color: rgb(224, 213, 166);\n"
"background-color: rgb(28, 31, 55);")
self.difficultyLbl.setObjectName("difficultyLbl")
self.verticalLayout.addWidget(self.difficultyLbl)
# Another spacer
spacerItem3 = QtWidgets.QSpacerItem(
20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(spacerItem3)
# A horizontal layout widget to keep the buttons in line
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
# Button to select easy difficulty
self.easyBtn = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.easyBtn.setStyleSheet("font: 8pt \"Kalam\";\n"
"color: rgb(224, 213, 166);\n"
"background-color: rgb(40, 119, 128);\n"
"border-color: rgb(28, 31, 55);")
self.easyBtn.setObjectName("easyBtn")
self.horizontalLayout.addWidget(self.easyBtn)
# Add click event to button
self.easyBtn.clicked.connect(self.easyMode)
# A horizontal spacer
spacerItem4 = QtWidgets.QSpacerItem(
40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem4)
# Button to select medium difficulty
self.mediumBtn = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.mediumBtn.setStyleSheet("font: 8pt \"Kalam\";\n"
"color: rgb(224, 213, 166);\n"
"background-color: rgb(40, 119, 128);\n"
"border-color: rgb(28, 31, 55);")
self.mediumBtn.setObjectName("mediumBtn")
self.horizontalLayout.addWidget(self.mediumBtn)
# Add click event to button
self.mediumBtn.clicked.connect(self.mediumMode)
# Another spacer
spacerItem5 = QtWidgets.QSpacerItem(
40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem5)
# Button to select hard difficulty
self.hardBtn = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.hardBtn.setStyleSheet("font: 8pt \"Kalam\";\n"
"color: rgb(224, 213, 166);\n"
"background-color: rgb(40, 119, 128);")
self.hardBtn.setObjectName("hardBtn")
self.horizontalLayout.addWidget(self.hardBtn)
# Add click event to button
self.hardBtn.clicked.connect(self.hardMode)
self.verticalLayout.addLayout(self.horizontalLayout)
# Add central widget (which contains all other widgets) to the main window
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow) # call retranslateUi method
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# retranslateUi method sets the text and titles of the widgets
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
# set window and widget titles:
MainWindow.setWindowTitle(_translate("Mastermind", "Mastermind"))
self.titleLbl.setText(_translate(
"MainWindow", "Welcome to Mastermind!"))
self.usernameLbl.setText(_translate("MainWindow", "Enter Your Name:"))
self.difficultyLbl.setText(_translate(
"MainWindow", "Choose Your Difficulty:"))
self.easyBtn.setText(_translate("MainWindow", "Easy"))
self.mediumBtn.setText(_translate("MainWindow", "Medium"))
self.hardBtn.setText(_translate("MainWindow", "Hard"))
# easyMode method will start the game on easy mode
def easyMode(self):
text = self.nameInp.toPlainText() # get inputted name
if text != "": # if user has inputted name
# hide menu widgets
MainWindow.hide()
# open easy window
easy.main()
# mediumMode method will start the game on medium mode
def mediumMode(self):
text = self.nameInp.toPlainText() # get inputted name
if text != "": # if user has inputted name
pass
# hardMode method will start the game on hard mode
def hardMode(self):
text = self.nameInp.toPlainText() # get inputted name
if text != "": # if user has inputted name
pass
# create a QApplication instance (passing in [] as I won't use CLI arguments):
app = QtWidgets.QApplication([])
# create a main application window:
MainWindow = QtWidgets.QMainWindow()
# create a UI for my window using MainMenu class:
ui = MainMenu(MainWindow)
MainWindow.show() # display window on screen
# ensures when exit button is pressed on window, program will exit at the end of a game loop:
sys.exit(app.exec_())
easy.py:
from PyQt5 import QtCore, QtGui, QtWidgets # import PyQt5
import sys # import sys
class EasyGame():
def __init__(self, EasyWindow):
EasyWindow.setObjectName("Mastermind") # set window title
EasyWindow.resize(800, 800) # resize window to be smaller
EasyWindow.setStyleSheet(
"background-color: rgb(125, 201, 197);") # set bg colour
# Set central widget for easy game
self.centralwidget = QtWidgets.QWidget(EasyWindow)
self.centralwidget.setObjectName("easywidget")
def main():
# create a QApplication instance (passing in [] as I won't use CLI arguments):
easyApp = QtWidgets.QApplication([])
# create a main application window:
EasyWindow = QtWidgets.QMainWindow()
# create a UI for my window using MainMenu class:
ui = EasyGame(EasyWindow)
EasyWindow.show() # display window on screen
# ensures when exit button is pressed on window, program will exit at the end of a game loop:
sys.exit(easyApp.exec_())