如何让按钮注册条目中输入的内容?

问题描述 投票:0回答:1

我有一些代码,我想要一个按钮来读取输入到条目中的内容,以便它检查输入的内容。这样,如果输入的项目错误,则会制作身体部位,但如果输入正确,则不会制作身体部位。 (HangMan) 我不确定我的代码当前有什么问题,也不知道如何解决这个问题。

现在,一切都不起作用,但没有显示任何错误。当我在条目中输入某些内容并按下按钮时,没有任何反应。

我添加

 def what_guess(): guess = txt.get()
只是因为代码不起作用,所以我想这可能是猜测。但除此之外...如果我把它去掉,代码仍然无法工作。单击按钮时没有任何反应...也许是按钮命令,但我不确定要添加什么,因为当我添加 check_guess(guess) 时,它说名称“word_with_blanks”尚未定义。

    #hangman

        # creates the man
        def update_hangman(maxguess):
            if maxguess == 5:
                canvas.create_oval(80, 206, 145, 145, fill = "black")
                canvas.create_oval(85, 201, 140, 150, fill = 'light gray')
            elif maxguess == 4:
                canvas.create_rectangle(105, 206, 120, 270, fill = "black")
            elif maxguess == 3:
                canvas.create_line(110, 220, 70, 250, width = 15)
            elif maxguess == 2:
                canvas.create_line(115, 220, 155, 250, width = 15)
            elif maxguess == 1:
                canvas.create_line(110, 268, 70, 330, width = 15)
            elif maxguess == 0:
                canvas.create_line(115, 268, 155, 330, width = 15)
                canvas.create_text(460, 400, text = "YOU", fill = "red", font = ("Arial", 50))
                canvas.create_text(500, 460, text = "LOSE!", fill = "red", font = ("Arial", 50))
        
        
        # checks the guess that was entered
        def check_guess(guess):
            global word_with_blanks
            if guess in secretWord:
                for i in range(len(secretWord)):
                    if secretWord[i] == guess:
                        word_with_blanks = word_with_blanks[:i] + guess + word_with_blanks[i+1:]
                word_label.configure(text = word_with_blanks )
                if '_' not in word_with_blanks:
                    end_game('Win')
            else:
                global maxguess
                maxguess -= 1
                update_hangman(maxguess)
                if maxguess == 0:
                    end_game('Lose')

        # checks to see what the results of the game is              
        def end_game(result):
            if result == 'Win':
                result_txt = "You Win!"
            else:
                result_text = 'You Lose, the word was ' + secretWord + '.'
                result_label.configure(text= result_text)
                txt.configure(state='disabled')
                buttontxt.configure(state='disabled')
                
        secretWord = random.choice(word_list)
        print (secretWord)
        
        # create word blanks
        word_with_blanks = '_' * len(secretWord)
        word_label = ttk.Label(self, text = word_with_blanks, font = (style4))
        word_label.place (x= 20, y = 384, anchor ='sw')
        word_label.configure(background='light gray')
        
        #create word entry
        word_label = ttk.Label(self, text = 'Answer Box', font = (style2))
        word_label.place (x= 435, y = 290, anchor ='sw')
        word_label.configure(background='light gray')
        txt = Entry(self)
        txt = Entry(self)
        txt.place(x=410, y=300, width = 170, height = 30)
        buttontxt = ttk.Button(self, text = 'Guess', style = 'T.TButton', command = check_guess(guess))
        buttontxt.place (x = 450, y = 350, width = 100, height = 40)
        style.configure('T.TButton', font =('calibri', 15, 'bold'))
        
        #results
        result_label = ttk.Label(self, font = (style2))
        result_label.place (x= 450, y = 350, anchor ='sw')
        
        maxguess=6
        update_hangman(maxguess)
python python-3.x tkinter tkinter-entry ttk
1个回答
0
投票

我看到的一个问题是您正在子类化

Toplevel
,但您的代码编写得就像在全局范围内一样,所以我首先建议将您的代码移至全局范围,以便它正常工作。

然后,为了解决阅读条目的问题,您可以简单地将

check_guess()
函数设置为按钮回调命令,并在函数顶部使用
txt.get()
从条目中抓取文本,然后您可以删除
guess 
来自回调的参数。

例如:

root = Tk() 
canvas= Canvas(root, width = 600, height = 400, bg = 'light gray')
label1=ttk.Label(root, text = 'Hang Man', font = (style4))
label1.place (x= 235, y = 50, anchor ='sw')
label1.configure(background='light gray')
button1 = ttk.Button(root, text = 'X', style = 'G.TButton', command = self.destroy)
button1.place (x = 550, y = 10, width = 35, height = 35)
style = ttk.Style()
style.configure('G.TButton', font =('calibri', 15, 'bold', 'underline'),foreground = 'red')
canvas.create_rectangle(300, 90, 325, 400, fill = "brown")
canvas.create_rectangle(100, 90, 300, 110, fill = "brown")
canvas.create_rectangle(100, 90, 120, 150, fill = "brown")
canvas.create_rectangle(170, 380, 400, 400, fill = "brown")
canvas.pack()

def update_hangman(maxguess):
    if maxguess == 5:
        canvas.create_oval(80, 206, 145, 145, fill = "black")
        canvas.create_oval(85, 201, 140, 150, fill = 'light gray')
    elif maxguess == 4:
        canvas.create_rectangle(105, 206, 120, 270, fill = "black")
    elif maxguess == 3:
        canvas.create_line(110, 220, 70, 250, width = 15)
    elif maxguess == 2:
        canvas.create_line(115, 220, 155, 250, width = 15)
    elif maxguess == 1:
        canvas.create_line(110, 268, 70, 330, width = 15)
    elif maxguess == 0:
        canvas.create_line(115, 268, 155, 330, width = 15)
        canvas.create_text(460, 400, text = "YOU", fill = "red", font = ("Arial", 50))
        canvas.create_text(500, 460, text = "LOSE!", fill = "red", font = ("Arial", 50))

def check_guess():
    global word_with_blanks
    guess = txt.get()
    if guess in secretWord:
        for i in range(len(secretWord)):
            if secretWord[i] == guess:
                word_with_blanks = word_with_blanks[:i] + guess + word_with_blanks[i+1:]
        word_label.configure(text = word_with_blanks )
        if '_' not in word_with_blanks:
            end_game('Win')
    else:
        global maxguess
        maxguess -= 1
        update_hangman(maxguess)
        if maxguess == 0:
            end_game('Lose')

# checks to see what the results of the game is
def end_game(result):
    if result == 'Win':
        result_txt = "You Win!"
    else:
        result_text = 'You Lose, the word was ' + secretWord + '.'
        result_label.configure(text= result_text)
        txt.configure(state='disabled')
        buttontxt.configure(state='disabled')

secretWord = random.choice(word_list)
print (secretWord)

# create word blanks
word_with_blanks = '_' * len(secretWord)
word_label = ttk.Label(root, text = word_with_blanks, font = (style4))
word_label.place (x= 20, y = 384, anchor ='sw')
word_label.configure(background='light gray')

#create word entry
word_label = ttk.Label(root, text = 'Answer Box', font = (style2))
word_label.place (x= 435, y = 290, anchor ='sw')
word_label.configure(background='light gray')
txt = Entry(self)
txt.place(x=410, y=300, width = 170, height = 30)
buttontxt = ttk.Button(root, text = 'Guess', style = 'T.TButton', command=check_guess)
buttontxt.place (x = 450, y = 350, width = 100, height = 40)
style.configure('T.TButton', font =('calibri', 15, 'bold'))

#results
result_label = ttk.Label(root, font = (style2))
result_label.place (x= 450, y = 350, anchor ='sw')

maxguess=6
update_hangman(maxguess)
root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.