简体   繁体   中英

Python GUI generate math equation

I have a homework question for one specific item with python GUIs.

My goal is to create a GUI that asks a random mathematical equation and if the equation is evaluated correctly, then I will receive a message stating that it is correct.

My main problem is finding out where to place my statements so that they show up in the labels; I have 1 textbox which generates the random equation, the next textbox is blank for me to enter the solution, and then an "Enter" button at the end to evaluate my solution.

It looks like this:

[*randomly generated equation*][*Empty space to enter solution*] [ENTER]

I've managed to get the layout and the evaluate parameters, but I don't know where to go from here.

This is my code so far:

class Equation(Frame):

    def __init__(self,parent=None):
        Frame.__init__(self, parent)
        self.pack()
        Equation.make_widgets(self)
        Equation.new_problem(self)

    def make_widgets(self):
        Label(self).grid(row=0, column=1)
        ent = Entry(self)
        ent.grid(row=0, column=1)
        Label(self).grid(row=0, column=2)
        ent = Entry(self)
        ent.grid(row=0, column=2)
        Button(self, text='Enter', command=self.evaluate).grid(row=0, column=3)

    def new_problem(self):
        pass

    def evaluate(self):
        result = eval(self.get())
        self.delete(0, END)
        self.insert(END, result)
        print('Correct')
self.labeltext = StringVar() # in __init__

# ...
Label(self, textvariable=self.labeltext) # in make_widgets

# ...
self.labeltext.set("Correct!") # in evaluate

In make_widgets() , you're creating a bunch of widgets, but not assigning them to any variables. This prevents you from being able to access them after you create them. Try assigning them to instance variables, eg:

def make_widgets(self):
        self.equation_label = Label(self)
        self.equation_label.grid(row=0, column=1) #notice that grid() is on another line
        self.entry1 = Entry(self)
        ent.grid(row=0, column=1)
        self.solution_label = Label(self)
        self.solution_label.grid(row=0, column=2)
        self.entry2 = Entry(self)
        ent.grid(row=0, column=2)
        self.button = Button(self, text='Enter', command=self.evaluate)
        self.button.grid(row=0, column=3)

That way, you could access them from other functions within the class like so:

self.solution_label.config(text="Hello World")

So your callback would end up looking more like this:

def evaluate(self):
        result = eval(self.get())
        self.solution_label.config(text=str(result))

For the Entry widget, you can either use JFSebastian's answer, or you can use the insert and delete methods (which it appears you were trying to do anyway):

def evaluate(self):
    #...some code...
    self.solution_entry.delete(0, END)
    self.solution_entry.insert(0, "Some text")
    #...more code...

Tkinterbook is a excellent resource for looking up widget configuration options and the like.

EDIT

See JFSebastian's answer for another way to set widget values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM