2017-08-06 53 views
-1
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_()) 

這是我目前正在研究的Hang子手遊戲。它是在Windows 7 32位機器上使用PyQt5和Python 3.5創建的。我遇到的問題是,當我點擊QPushButton'btn'時,應用程序關閉,我找不到原因。它沒有完成,有很多代碼缺失,但我認爲它有它需要做我想做的事情,但它不起作用。歡迎任何幫助/建議。 :)爲什麼我的QPushButton在PyQt5中關閉應用程序?

回答

0

主要問題是您在方法中創建的變量僅存在於該區域中,例如,單詞只存在於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) 
+0

非常感謝你,這解決了我的問題。也非常感謝你的更正和優化。 :) –

相關問題