Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 298
Nero Dietrich Avatar asked Sep 14 '26 23:09

Nero Dietrich


1 Answers

self.labeltext = StringVar() # in __init__

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

# ...
self.labeltext.set("Correct!") # in evaluate
like image 186
jfs Avatar answered Sep 17 '26 14:09

jfs