按钮单击时标签不会更新

时间:2012-10-08 04:05:39

标签: python tkinter

我们必须为小学生测试那里的数学技能。我需要左边的标签有一个随机方程,右边有一个输入表格,最右边有一个按钮,检查他们给出的答案是否正确。如果他们做对了,就给出了一个新的等式。

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 tho我希望它改变为新的等式。

1 个答案:

答案 0 :(得分:1)

在实例化new_problem对象时,您只调用Ed。它之后再也没有被调用过,这样做实际上不会改变现有的设置,因为你只需要在实例化时为你的小部件创建一次标签。

您需要做的是将tkinter.StringVar传递到Label窗口小部件,然后set传递该变量的算术问题。然后在每次成功回答后运行new_problem

我稍微更新了你的代码以使用super并直接在实例上而不是通过类运行方法,但这应该有效:

from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END, TOP, END, StringVar
from tkinter.messagebox import showinfo
from random import randrange

class Ed(Frame):
    """Simple arithmetic education app"""
    def __init__(self,parent=None):
        """constructor"""
        super().__init__(parent)
        self.tries = 0
        self.problem = StringVar()
        self.make_widgets()
        self.new_problem()
        self.pack()

    def make_widgets(self):
        """defines Ed widgets"""
        self.label = Label(self, textvariable=self.problem)
        self.label.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.tries = 0
        self.a = randrange(1,10,1)
        self.b = randrange(1,10,1)
        if randrange(1,10,1) < 5:
            self.total = self.a + self.b
            self.plusminus = '+'
        else:
            self.total = self.a - self.b
            self.plusminus = '-'
        self.problem.set( (self.a , self.plusminus, self.b) )

    def evaluate(self):
        """handles button "Enter" clicks by comparing answer in entry to correct result"""
        if self.total == int(self.ent.get()):
            showinfo(title='YAY', message='You are CORRECT!')
            self.ent.delete(0,END)
            self.new_problem()
        else:
            self.ent.delete(0,END)
            self.ent.insert(END, 'Wrong. Try again.')
            self.tries += 1

Ed().mainloop()

我还将您的eval更改为int ...您真的不想这样做,因为它可以让用户注入python代码进入你的申请。

例如,我可以输入以下内容作为我的“答案”:

showinfo(title="Blah", message="Look ma - No hands!")

这只会打开一个简单的信息框,但是门是敞开的,更多涉及恶作剧。