为什么我的QPushButton在PyQt5中关闭应用程序?

时间:2017-08-06 08:47:30

标签: python python-3.x pyqt pyqt5 qpushbutton

from sys import (exit, argv)
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QToolTip, QPushButton, QApplication, QWidget, QLabel, QLineEdit)
from PyQt5.QtGui import (QIcon, QPixmap, QFont)
from random import choice

#Word list for the words the user will attempt to guess
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']

#Creates the main widget which will contain everything else
class hangman(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        #Creates the QLabel 'background' which will contain the white background
        self.background = QLabel(self)
        #Uses QPixmap to place the background into the QLabel 'background'
        self.background.setPixmap(QPixmap('background.jpg').scaled(201, 352, Qt.IgnoreAspectRatio, Qt.FastTransformation))
        self.background.move(0.5, 0.5)

        #Creates the QLabel 'image' which will contain the image of the hangman
        self.image = QLabel(self)
        number = '1'
        #Uses QPixmap to insert the image of the hangman into the QLabel 'image'
        self.image.setPixmap(QPixmap('hangman_' + number + '.png').scaled(100, 200, Qt.KeepAspectRatio, Qt.FastTransformation))
        self.image.move(60, 0.5)

        #Chooses random word from list 'words'
        word = choice(words)  
        #Creates a blank version of the chosen word 
        blank_word = ''
        for i in word:
            blank_word += '__ '
        blank_word.rstrip()
        guessed_letters = []

        self.blank_word_label = QLabel(blank_word, self)
        self.blank_word_label.setFixedWidth(200)
        self.blank_word_label.move(0,200)
        self.blank_word_label.setAlignment(Qt.AlignCenter)

        self.btn = QPushButton('Check', self)
        #Selects the font/font size for the label on the QPushButton 'btn' using QFont
        self.btn.setFont(QFont('SansSerif', 20))
        #Creates a tooltip when user hovers over the QPushButton 'btn' using QToolTip
        self.btn.setToolTip('Click to check if the entered letter is in the word')
        #Selects the font/font size for the QToolTip above on the QPushButton 'btn' using QFont
        QToolTip.setFont(QFont('SansSerif', 10))
        #Connects the QPushButton 'btn' to the function 'check_letter' to activate when the button is clicked
        self.btn.clicked.connect(self.check_letter)
        self.btn.resize(102, 43)
        self.btn.move(99, 228)

        self.entered_letter = QLineEdit(self)
        font = self.entered_letter.font()
        font.setPointSize(24)
        self.entered_letter.setFont(font)
        self.entered_letter.setMaxLength(1)
        self.entered_letter.setToolTip('Enter a letter and check if it is in the word')
        self.entered_letter.resize(100, 43)
        self.entered_letter.move(0.5, 228)

        #Sets where on the screen the window will open and the size of the window respectively using x and y coordinates
        self.setGeometry(1390, 30, 200, 270)
        #Locks the size of the window and make it impossible for the user to change it
        self.setFixedSize(self.size())
        self.setWindowTitle('Hangman')
        #Sets the window icon to the image file 'icon.png' located in the same folder as the source file
        self.setWindowIcon(QIcon('icon.png'))      
        self.show()

    def check_letter(self):
        if self.entered_letter.text() in word:
            guessed_letters.append(self.entered_letter.text())

        else:
            number = int(number)
            number += 1
            number = str(number)
            self.image.setPixmap(QPixmap('hangman_' + number + '.png').scaled(100, 200, Qt.KeepAspectRatio, Qt.FastTransformation))
            QApplication.processEvents()

        blank_word = ''
        for i in word:
            if i in guessed_letters:
                blank_word += i

            else:
                blank_word += '__ '

            blank_word.rstrip()

        self.blank_word_label = QLabel(blank_word, self)
        QApplication.processEvents()



if __name__ == '__main__':

    #Begins the execution of the QApplication

    app = QApplication(argv)
    ex = hangman()
    ex.show()
    exit(app.exec_())  

这是我目前正在进行的Hangman游戏。它是在Windows 7 32位机器上使用PyQt5和Python 3.5创建的。我遇到的问题是,当我点击QPushButton'btn'时,应用程序关闭,我无法弄清楚原因。它没有完成,并且有很多代码缺失,但我认为它有什么需要做我想做的事情,但它不起作用。欢迎任何帮助/建议。 :)

1 个答案:

答案 0 :(得分:0)

主要问题是你在方法中创建的变量只存在于该区域中,例如word只存在于initUI中,但你想在check_letter中使用它,这就是你的程序崩溃的原因。我已经纠正了这些错误,除了纠正一些逻辑之外,另一个错误是你正在创建一个新的QLabel,而你必须更新文本。

class hangman(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        #Creates the QLabel 'background' which will contain the white background
        self.background = QLabel(self)
        #Uses QPixmap to place the background into the QLabel 'background'
        self.background.setPixmap(QPixmap('background.jpg').scaled(201, 352, Qt.IgnoreAspectRatio, Qt.FastTransformation))
        self.background.move(0.5, 0.5)

        #Creates the QLabel 'image' which will contain the image of the hangman
        self.image = QLabel(self)
        self.number = 1
        #Uses QPixmap to insert the image of the hangman into the QLabel 'image'
        self.image.setPixmap(QPixmap('hangman_{}.png'.format(self.number)).scaled(100, 200, Qt.KeepAspectRatio, Qt.FastTransformation))
        self.image.move(60, 0.5)
        #Chooses random word from list 'words'
        self.word = choice(words)  

        #Creates a blank version of the chosen word 
        blank_word = '__ '*len(self.word)

        self.guessed_letters = ""

        self.blank_word_label = QLabel(blank_word, self)
        self.blank_word_label.setFixedWidth(200)
        self.blank_word_label.move(0,200)
        self.blank_word_label.setAlignment(Qt.AlignCenter)

        self.btn = QPushButton('Check', self)
        #Selects the font/font size for the label on the QPushButton 'btn' using QFont
        self.btn.setFont(QFont('SansSerif', 20))
        #Creates a tooltip when user hovers over the QPushButton 'btn' using QToolTip
        self.btn.setToolTip('Click to check if the entered letter is in the word')
        #Selects the font/font size for the QToolTip above on the QPushButton 'btn' using QFont
        QToolTip.setFont(QFont('SansSerif', 10))
        #Connects the QPushButton 'btn' to the function 'check_letter' to activate when the button is clicked
        self.btn.clicked.connect(self.check_letter)
        self.btn.resize(102, 43)
        self.btn.move(99, 228)

        self.entered_letter = QLineEdit(self)
        font = self.entered_letter.font()
        font.setPointSize(24)
        self.entered_letter.setFont(font)
        self.entered_letter.setMaxLength(1)
        self.entered_letter.setToolTip('Enter a letter and check if it is in the word')
        self.entered_letter.resize(100, 43)
        self.entered_letter.move(0.5, 228)

        #Sets where on the screen the window will open and the size of the window respectively using x and y coordinates
        self.setGeometry(1390, 30, 200, 270)
        #Locks the size of the window and make it impossible for the user to change it
        self.setFixedSize(self.size())
        self.setWindowTitle('Hangman')
        #Sets the window icon to the image file 'icon.png' located in the same folder as the source file
        self.setWindowIcon(QIcon('icon.png'))      
        self.show()

    def check_letter(self):

        if self.entered_letter.text() in self.word:
            self.guessed_letters += self.entered_letter.text()
        else:
            self.number += 1
            self.image.setPixmap(QPixmap('hangman_{}.png'.format(self.number)).scaled(100, 200, Qt.KeepAspectRatio, Qt.FastTransformation))

        blank_word = ''
        for i in self.word:
            if i in self.guessed_letters:
                blank_word += i
            else:
                blank_word += '__ '

        self.blank_word_label.setText(blank_word)