程序崩溃时按下按钮 - 数字猜游戏 - Tkinter

时间:2017-10-19 22:36:56

标签: python-3.x tkinter

我不知道为什么这不起作用,我一直在寻找,我不知道什么是错的我已经搞砸了一段时间我想要的就是让它工作。 得到输入输出数字等于随机生成如果全数等于随机然后你赢了但是它只是在我按下按钮时崩溃

from tkinter import *
from tkinter import ttk
import random
master = Tk()
master.title('Guess The Number!')
global answer
global guess_entry
global guess_display
answer = str(random.randint(1000,9999))
guess_counter = 0
def callback():
    print('Button pressed')
    counter = 0

    correct = []
    while counter < 4:
        if guess_entry.get() == answer:
            correct.append('Well done, that is correct')
            break
        elif guess_entry.get()[counter] == answer[counter]:
            correct.append(guess_entry.get[counter])

            counter += 1
    guess_display['text'] = '  '.join(str(correct))

def Help():
    win = Toplevel()
    win.title('Help')

    l = Label(win, text="Guess a 4 digit number and I will tell you\n what you got right,\n keep trying until you get it in the \ncorrect order with correct numbers")
    l.grid(row=0, column=0)

    b = Button(win, text="Okay", command=win.destroy)
    b.grid(row=1, column=0)

guess_entry = Entry(master)

guess_check = Button(master, text='Guess', command=callback)

guess_display = Label(master,text='_  _  _  _')


help_button = ttk.Button(master, text="?", command=Help,width=3)

guess_entry.grid(row=0,column=2)
guess_check.grid(row=1,column=2)

guess_display.grid(row=2,column=1)

help_button.grid(row=0,column=4)
master.mainloop()

1 个答案:

答案 0 :(得分:0)

如果数字在循环中第一次不相等,那么它们在循环中的任何其他时间都不会相等,因为在循环运行时用户将无法更改其答案。

您可以通过添加print语句来看到这一点:

while counter < 4:
    print("counter:", counter, "guess:", guess_entry.get())
    ...