calculator.py help
John Grayson
johngrayson at home.com
Tue Nov 14 09:13:47 EST 2000
In article <8uqrlv$f5b$1 at nnrp1.deja.com>,
dijkhuis at my-deja.com wrote:
> Hello,
>
> Can someone please help me with the following code?
> I found this example somewhere but it won't work.
> It is supposed to be a calculator.
> Thanks,
>
> Dijkhuis
> ------
>
> from Tkinter import *
> from math import *
>
> def evaluate(event):
> label['text']="result: " + str(eval(expression.get()))
>
> frame = Frame(None)
>
> entry = Entry(frame)
> entry['textvariable'] = expression = StringVar()
> entry.bind("",evaluate)
>
> label = Label(frame)
>
> button = Button(frame, text = "Submit", command = evaluate)
>
> frame.pack()
> entry.pack()
> label.pack()
> button.pack()
> frame.mainloop()
>
Rearranging your code - this works...
from Tkinter import *
from math import *
class Calculator:
def __init__(self, master):
self.frame = Frame(master)
self.entry = Entry(self.frame)
self.entry['textvariable'] = self.expression = StringVar()
self.entry.bind("=",self.evaluate)
self.label = Label(self.frame)
self.button = Button(self.frame, text = "Submit",
command = self.evaluate)
self.frame.pack()
self.entry.pack()
self.label.pack()
self.button.pack()
def evaluate(self, event=None):
self.label['text']="result: " +
str(eval(self.expression.get()))
root = Tk()
calc = Calculator(root)
root.mainloop()
John Grayson
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Python-list
mailing list