将 tkinter Entry 与实际添加答案进行比较
Compare tkinter Entry to actual addition answer
我正在尝试将输入框的输入与实际答案进行比较。这是我的第一个 python 项目,我仍然很困惑,老实说,我什至不知道如何开始问这个问题。
用户将单击加法或减法按钮。将出现一个字符串询问 "What does 4 + 5 equal?" 数字是随机生成的。
然后我使用 tkinter 库插入一个输入框。 我不知道如何get()
输入并将其与实际数字的和或差进行比较。
我试图遵循这个 video 但我也没有使用其他方法。仅供参考,我主要关注的是加法,所以如果你测试的话,先加法。
from tkinter import Entry
import tkinter as tk
import random as rand
root = tk.Tk()
root.geometry("450x450+500+300")
root.title("Let's play Math!")
welcomeLabel = tk.Label(text="LET'S PLAY MATH!").pack()
startLabel = tk.Label(text='Select a math operation to start').pack()
def addition():
a = rand.randrange(1,10,1)
b = rand.randrange(1,10,1)
global Answer
Answer = a + b
tk.Label(text="What does " + str(a) + " + " + str(b) + " equal? ").place(x=0, y=125)
global myAnswer
myAnswer = Entry().place(x=300, y= 125)
def checkAnswer():
entry = myAnswer.get()
while int(entry) != Answer:
if int(entry) != Answer:
tk.Label(text="Let's try again.").pack()
elif int(entry) == Answer:
tk.Label(text="Hooray!").pack()
addBtn = tk.Button(text="Addition", command=addition).place(x=100, y = 60)
subBtn = tk.Button(text="Subtraction", command=subtraction).place(x=200, y=60)
checkBtn = tk.Button(text="Check Answer", command=checkAnswer).place(x=300, y = 150)
tk.mainloop()
要获得答案的值,请执行 answer = myanswer.get()
或任何其他变量名称。要将其与正确答案进行比较,请执行
if int(answer) == correctAnswer:
#the code
这就是你要问的吗?
考虑 ,下面是一个示例,当用户点击 [=] 时,响应输入 answer
的数字是 "Correct!"
还是 "False!"
14=]:
import tkinter as tk
import random as rand
def is_correct():
global answer, check
if answer.get() == str(a + b):
check['text'] = "Correct!"
else:
check['text'] = "False!"
def restart():
global question, check
random_nums()
question['text'] = "{}+{}".format(a, b)
check['text'] = "Please answer the question!"
def random_nums():
global a, b
a = rand.randrange(1, 10, 1)
b = rand.randrange(1, 10, 1)
root = tk.Tk()
#create widgets
question = tk.Label(root)
answer = tk.Entry(root, width=3, justify='center')
check = tk.Label(root)
tk.Button(root, text="Check", command=is_correct).pack()
tk.Button(root, text="New question", command=restart).pack()
#layout widgets
question.pack()
answer.pack()
check.pack()
restart()
root.mainloop()
解决问题所需要做的就是将 Entry()
对象的创建与放置分开:
def addition():
a = rand.randrange(1,10,1)
b = rand.randrange(1,10,1)
global Answer
Answer = int(a + b)
tk.Label(text="What does " + str(a) + " + " + str(b) + " equal? ").place(x=0, y=125)
global myAnswer
myAnswer = Entry()
myAnswer.place(x=300, y= 125)
Entry()
returns 入口对象,它有一个 get()
方法。但是,当您链接 .place()
时,您会 return 它的结果,即 None
。因此,您实际上从未将 Entry
对象存储在变量中。
此外,最好确保 Answer
也是 int
。
我正在尝试将输入框的输入与实际答案进行比较。这是我的第一个 python 项目,我仍然很困惑,老实说,我什至不知道如何开始问这个问题。
用户将单击加法或减法按钮。将出现一个字符串询问 "What does 4 + 5 equal?" 数字是随机生成的。
然后我使用 tkinter 库插入一个输入框。 我不知道如何get()
输入并将其与实际数字的和或差进行比较。
我试图遵循这个 video 但我也没有使用其他方法。仅供参考,我主要关注的是加法,所以如果你测试的话,先加法。
from tkinter import Entry
import tkinter as tk
import random as rand
root = tk.Tk()
root.geometry("450x450+500+300")
root.title("Let's play Math!")
welcomeLabel = tk.Label(text="LET'S PLAY MATH!").pack()
startLabel = tk.Label(text='Select a math operation to start').pack()
def addition():
a = rand.randrange(1,10,1)
b = rand.randrange(1,10,1)
global Answer
Answer = a + b
tk.Label(text="What does " + str(a) + " + " + str(b) + " equal? ").place(x=0, y=125)
global myAnswer
myAnswer = Entry().place(x=300, y= 125)
def checkAnswer():
entry = myAnswer.get()
while int(entry) != Answer:
if int(entry) != Answer:
tk.Label(text="Let's try again.").pack()
elif int(entry) == Answer:
tk.Label(text="Hooray!").pack()
addBtn = tk.Button(text="Addition", command=addition).place(x=100, y = 60)
subBtn = tk.Button(text="Subtraction", command=subtraction).place(x=200, y=60)
checkBtn = tk.Button(text="Check Answer", command=checkAnswer).place(x=300, y = 150)
tk.mainloop()
要获得答案的值,请执行 answer = myanswer.get()
或任何其他变量名称。要将其与正确答案进行比较,请执行
if int(answer) == correctAnswer:
#the code
这就是你要问的吗?
考虑 answer
的数字是 "Correct!"
还是 "False!"
14=]:
import tkinter as tk
import random as rand
def is_correct():
global answer, check
if answer.get() == str(a + b):
check['text'] = "Correct!"
else:
check['text'] = "False!"
def restart():
global question, check
random_nums()
question['text'] = "{}+{}".format(a, b)
check['text'] = "Please answer the question!"
def random_nums():
global a, b
a = rand.randrange(1, 10, 1)
b = rand.randrange(1, 10, 1)
root = tk.Tk()
#create widgets
question = tk.Label(root)
answer = tk.Entry(root, width=3, justify='center')
check = tk.Label(root)
tk.Button(root, text="Check", command=is_correct).pack()
tk.Button(root, text="New question", command=restart).pack()
#layout widgets
question.pack()
answer.pack()
check.pack()
restart()
root.mainloop()
解决问题所需要做的就是将 Entry()
对象的创建与放置分开:
def addition():
a = rand.randrange(1,10,1)
b = rand.randrange(1,10,1)
global Answer
Answer = int(a + b)
tk.Label(text="What does " + str(a) + " + " + str(b) + " equal? ").place(x=0, y=125)
global myAnswer
myAnswer = Entry()
myAnswer.place(x=300, y= 125)
Entry()
returns 入口对象,它有一个 get()
方法。但是,当您链接 .place()
时,您会 return 它的结果,即 None
。因此,您实际上从未将 Entry
对象存储在变量中。
此外,最好确保 Answer
也是 int
。