将python hangman转换为GUI

时间:2018-05-06 11:29:35

标签: python python-3.x tkinter python-requests subprocess

我正在制作关于创建猜词的项目。但我对于应该在labelGuessResult上添加的循环和字母有疑问。请帮我解决我的代码并帮助我理解我的代码有什么问题。我希望获得的结果是labelGuessResult中显示正确的猜测字母,每当我输入正确的猜测时,它将添加如此_ren

from tkinter import *
import tkinter.messagebox
from random import choice
randomWords = choice(["yeti", "siren", "lich"])
lives = 7
guessedWord = []
guessedWrong = []

#defining the main class
class window(Frame):
#defining main window initialization
def __init__(self, master=None):

    Frame.__init__(self, master)
    self.master = master
    self.init_window()

#defining main frame
def init_window(self):

    self.master.title("H A N G M A N")
    self.master.resizable(False, False)
    self.pack(fill=BOTH, expand=1)


    self.label1 = Label(self, text="Guessed the %s letter word: " % (len(randomWords)))
    self.label1.grid(row=1, column=1, ipadx=5, ipady=5, sticky=W)

    '''DISPLAY WORD'''
    self.labelGuessResult = Label(self, text=len(randomWords)* " _")
    self.labelGuessResult.grid(row=1, column=2)

    '''GUESS INPUT'''
    self.guessEnt = Entry(self)
    self.guessEnt.grid(row=1, column=3)
    guess = self.guessEnt.get()


    self.label2 = Label(self, text="Message: ")
    self.label2.grid(row=2, column=1, ipadx=5, ipady=5, sticky=W)
    self.labelMessage = Label(self, text=guessedWord + guessedWrong)
    self.labelMessage.grid(row=2, column=2)

    self.label3 = Label(self, text="Guessed Letters: ")
    self.label3.grid(row=3, column=1, ipadx=5, ipady=5, sticky=W)
    self.lblGuessed = Label(self, text=guessedWord)
    self.lblGuessed.grid(row=3, column=2)

    self.label4 = Label(self, text="Lives left: ")
    self.label4.grid(row=4, column=1, ipadx=5, ipady=5, sticky=W)
    self.lblLives = Label(self, text=lives)
    self.lblLives.grid(row=4, column=2)

    def checkWord():
        while True:
            out = ""
            for letter in randomWords:
                if letter in guessedWord:
                    self.labelGuessResult.config(text=out+letter)
                else:
                    self.labelGuessResult.config(text=out+" _")
            if out == randomWords:
                self.labelMessage.config("You have guesed the ", randomWords)
                break
            if guess in guessedWord:
                self.labelMessage.config(text="already guessed that!")
            elif guess in randomWords:
                self.labelMessage.config(text="correct!")
                guessedWord.append(guess)
            else:
                self.labelMessage.config(text="Wrong!")
                guessedWrong.append(guess)
            print()

    '''BUTTON GUESS'''
    self.submitBTN = Button(self, text="Guess", command=checkWord)
    self.submitBTN.grid(row=1,column=4)


frames = Tk()              #creates the frame
frames.geometry("500x450") #sets frame size
app = window(frames)       #calls and runs the frame within the class
frames.mainloop()          #runs the frame continuously

1 个答案:

答案 0 :(得分:0)

嗯,一个主要问题是checkWord()功能。第一次调用此函数时,它会启动一个我很难理解的循环,而且似乎没有结束。

这意味着,当您致电checkWord()后,您将无法输入更多字母。

我的建议是首先制作一个猜测和检查程序的流程图,然后一点一点地实现它,同时检查你得到了正确的回报。首先,跳过GUI可能更简单,因为它只会增加复杂性,直到您开始工作。