我是PyQt5的新手,这是我在python和PyQt中的第一个项目。问题是这样的:当我单击屏幕上的任何按钮(字母按钮)时,程序将关闭immidietley。我不知道我的错误在哪里,这是什么。在控制台中未显示任何信息。如果您有答案,请回答。谢谢!
import sys, random, os
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from random import choice
words = ['captivity', 'america', 'europe', 'federal', 'gluten', 'ridiculous', 'automatic', 'television', 'difficult', 'severe', 'interesting', 'indonesia', 'industrial',
'automotive', 'president', 'terrestrial', 'academic', 'comedic', 'comical', 'genuine', 'suitcase', 'vietnam', 'achievement', 'careless', 'monarchy', 'monetary',
'quarantine', 'supernatural', 'illuminate', 'optimal', 'application', 'scientist', 'software', 'hardware', 'program', 'colonial', 'algorithm', 'intelligent',
'electricity', 'verification', 'broadband', 'quality', 'validation', 'online', 'telephone', 'dictionary', 'keyboard', 'china', 'london', 'jamaica', 'biology',
'chemistry', 'history', 'historian', 'africa', 'mathematics', 'computer', 'literature', 'gravity', 'guitar', 'violin', 'illuminate', 'england', 'china', 'japan',
'canada', 'suitcase', 'wireless', 'internet']
word = choice(words)
blank_word = '_ ' * len(word)
blank_word.rstrip()
guess = None
guessedLetter = ""
chances = 6
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("HangMan Game!")
self.setWindowIcon(QIcon(os.path.join('icons', 'Hangman-icon.png')))
self.initUI()
self.setFixedSize(500, 450)
self.show()
def initUI(self):
label5 = QLabel(blank_word, self)
label5.move(200, 200)
#Buttons
buttonQ = QPushButton('q', self)
buttonQ.move(20, 325)
buttonQ.resize(40,25)
buttonQ.clicked.connect(self.buttonClicked)
buttonW = QPushButton('w', self)
buttonW.move(66.66, 325)
buttonW.resize(40,25)
buttonW.clicked.connect(self.buttonClicked)
buttonE = QPushButton('e', self)
buttonE.move(113.32, 325)
buttonE.resize(40,25)
buttonE.clicked.connect(self.buttonClicked)
buttonR = QPushButton('r', self)
buttonR.move(159.98, 325)
buttonR.resize(40,25)
buttonR.clicked.connect(self.buttonClicked)
buttonT = QPushButton('t', self)
buttonT.move(206.64, 325)
buttonT.resize(40,25)
buttonT.clicked.connect(self.buttonClicked)
buttonY = QPushButton('y', self)
buttonY.move(253.30, 325)
buttonY.resize(40,25)
buttonY.clicked.connect(self.buttonClicked)
buttonU = QPushButton('u', self)
buttonU.move(299.96, 325)
buttonU.resize(40,25)
buttonU.clicked.connect(self.buttonClicked)
buttonI = QPushButton('i', self)
buttonI.move(346.62, 325)
buttonI.resize(40,25)
buttonI.clicked.connect(self.buttonClicked)
buttonO = QPushButton('o', self)
buttonO.move(393.28, 325)
buttonO.resize(40,25)
buttonO.clicked.connect(self.buttonClicked)
buttonP = QPushButton('p', self)
buttonP.move(439.94, 325)
buttonP.resize(40,25)
buttonP.clicked.connect(self.buttonClicked)
buttonA = QPushButton('a', self)
buttonA.move(43.36, 355)
buttonA.resize(40,25)
buttonA.clicked.connect(self.buttonClicked)
buttonS = QPushButton('s', self)
buttonS.move(90.02, 355)
buttonS.resize(40,25)
buttonS.clicked.connect(self.buttonClicked)
buttonD = QPushButton('d', self)
buttonD.move(136.68, 355)
buttonD.resize(40,25)
buttonD.clicked.connect(self.buttonClicked)
buttonF = QPushButton('f', self)
buttonF.move(183.34, 355)
buttonF.resize(40,25)
buttonF.clicked.connect(self.buttonClicked)
buttonG = QPushButton('g', self)
buttonG.move(230, 355)
buttonG.resize(40,25)
buttonG.clicked.connect(self.buttonClicked)
buttonH = QPushButton('h', self)
buttonH.move(276.66, 355)
buttonH.resize(40,25)
buttonH.clicked.connect(self.buttonClicked)
buttonJ = QPushButton('j', self)
buttonJ.move(323.32, 355)
buttonJ.resize(40,25)
buttonJ.clicked.connect(self.buttonClicked)
buttonK = QPushButton('k', self)
buttonK.move(369.98, 355)
buttonK.resize(40,25)
buttonK.clicked.connect(self.buttonClicked)
buttonL = QPushButton('l', self)
buttonL.move(416.64, 355)
buttonL.resize(40,25)
buttonL.clicked.connect(self.buttonClicked)
buttonZ = QPushButton('z', self)
buttonZ.move(90.02, 385)
buttonZ.resize(40,25)
buttonZ.clicked.connect(self.buttonClicked)
buttonX = QPushButton('x', self)
buttonX.move(136.68, 385)
buttonX.resize(40,25)
buttonX.clicked.connect(self.buttonClicked)
buttonC = QPushButton('c', self)
buttonC.move(183.34, 385)
buttonC.resize(40,25)
buttonC.clicked.connect(self.buttonClicked)
buttonV = QPushButton('v', self)
buttonV.move(230, 385)
buttonV.resize(40,25)
buttonV.clicked.connect(self.buttonClicked)
buttonB = QPushButton('b', self)
buttonB.move(276.66, 385)
buttonB.resize(40,25)
buttonB.clicked.connect(self.buttonClicked)
buttonN = QPushButton('n', self)
buttonN.move(323.32, 385)
buttonN.resize(40,25)
buttonN.clicked.connect(self.buttonClicked)
buttonM = QPushButton('m', self)
buttonM.move(369.98, 385)
buttonM.resize(40,25)
buttonM.clicked.connect(self.buttonClicked)
#Image
label = QLabel(self)
pixmap = QPixmap('hangman0.png')
label.setPixmap(pixmap)
label.move(20, 40)
label.resize(131, 222)
self.resize(pixmap.width(),pixmap.height())
#Buttons actions
def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
if chances != 0 and "_ " in blank_word:
label2 = QLabel("\nYou have {} tries left", self).format(chances)
label2.move(200, 40)
guess = sender.text()
print(guess)
else:
if guess in guessedLetter:
self.statusBar().showMessage("You already guessed that letter, try another one")
else:
pass
guessedLetter.append(guess)
if guess not in word:
chances -= 1
label = QLabel(self)
pixmap = QPixmap('hangman{}.png').format(6 - (chances))
label.setPixmap(pixmap)
label.move(20, 40)
label.resize(131, 222)
self.resize(pixmap.width(),pixmap.height())
else:
searchMore = True
while searchMore:
try:
foundAtIndex = word.index(guess, startsearchIndex)
blank_word[foundAtIndex] = guess
startsearchIndex = foundAtIndex + 1
except:
searchMore = False
label5 = QLabel("".join(blank_word), self)
label5.move(200, 200)
if chances == 0:
label2 = QLabel("You lose, the word was " + word, self)
label2.move(200, 40)
if "_ " not in blank_word:
label2 = QLabel("Congratulation you won!", self)
label2.move(200, 40)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
window.resize(500, 450)
app.exec_()