2017-08-06 3 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_()) 

これは私が現在取り組んでいるハングマンゲームです。これは、Windows 7の32ビットマシンでPyQt5とPython 3.5を使用して作成されています。私が抱えている問題は、QPushButton 'btn'をクリックするとアプリケーションが終了し、その理由がわからないということです。それは仕上げではなく、多くのコードがありませんが、私はそれが私がしたいことをする必要があると思うが、それは動作しません。助けやアドバイスは大歓迎です。 :)私のQPushButtonがPyQt5のアプリケーションを閉じるのはなぜですか?

答えて

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) 
+0

ありがとうございます、これは私の問題を解決しました。また、訂正と最適化に非常に感謝します。 :) –

関連する問題