我们必须为小学生做一个测试,以测试他们的数学技能。我需要左边的标签有一个随机方程,右边有一个输入表格,最右边有一个按钮来检查他们给出的答案是否正确。如果他们做对了,就会给出一个新的等式。
from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END, TOP, END
from tkinter.messagebox import showinfo
from random import randrange
class Ed(Frame):
'Simple arithmetic education app'
def __init__(self,parent=None):
'constructor'
Frame.__init__(self, parent)
self.pack()
Ed.new_problem(self)
Ed.make_widgets(self)
self.tries = 0
def make_widgets(self):
'defines Ed widgets'
if self.plusminus == 1:
Label(self, text=(self.a ,"+" ,self.b)).pack(side=LEFT)
else:
Label(self, text=(self.a, "-" ,self.b)).pack(side=LEFT)
self.ent = Entry(self)
self.ent.pack(side=LEFT)
Button(self, text='Enter', command=self.evaluate).pack(side=RIGHT)
def new_problem(self):
'creates new arithmetic problem'
self.a = randrange(1,10,1)
self.b = randrange(1,10,1)
self.c = randrange(1,10,1)
if self.c < 5:
self.total = self.a + self.b
self.plusminus = 1 #plusminus = plus
else:
self.total = self.a - self.b
self.plusminus = 0 #plusminus = minus
def evaluate(self):
'handles button "Enter" clicks by comparing answer in entry to correct result'
if self.total == eval(self.ent.get()):
showinfo(title='YAY', message='You are CORRECT!')
self.ent.delete(0,END)
else:
self.ent.delete(0,END)
self.ent.insert(END, 'Wrong. Try again.')
self.tries+=1
Ed().mainloop()
问题是当答案正确时等式不会更新。所以当我输入 12 时,问题是 8 + 4。它是正确的,但保持 8 + 4,但我希望它改变为一个新的方程。