我正在尝试在Tkinter中编写一个测验应用程序,到目前为止我已经得到了下面的代码 - 但即使我输入正确的答案,它总是会出现错误。
import tkinter
from random import randint
score = 0
def question():
global number1
global number2
number1 = randint(1,12)
number2 = randint(1,12)
question = tkinter.Label(window, text="What is " + str(number1) + "x" + str(number2) + " ?")
question.pack()
def Validate():
global score
global number1
global number2
ans = number1 * number2
if answer == number1*number2:
score = score+1
print('True')
else:
score = score-1
print('False')
window = tkinter.Tk()
question()
answer = tkinter.Entry(window)
submit = tkinter.Button(window, text = "Submit", command=Validate)
answer.pack()
submit.pack()
window.mainloop()
答案 0 :(得分:3)
正如Rawing评论的那样,代码将tkinter.Entry
对象与对象进行比较。
您可以使用Entry.get()
将输入的文字输入到条目中,您可以使用int
将字符串转换为int()
对象:
def Validate():
global score
answer_string = answer.get()
try:
answer_value = int(answer_string)
except ValueError as e:
print(e)
return
if answer_value == number1 * number2:
score -= 1
print('True')
else:
score += 1
print('False')