[Tutor] Simple calculator

Kent Johnson kent37 at tds.net
Wed Nov 1 12:22:07 CET 2006


Joe Cox wrote:
> I found this simple calculator on the web:
> 
> from Tkinter import *
> from math import *
> ###http://sunsite.uakom.sk/sunworldonline/swol-02-1998/swol-02-python.htmlBy
> Cameron Laird and Kathryn Soraiz...Getting Started with Python###
> 
> def evaluate(event):
>         label['text'] = "Result:  " + str(eval(expression.get()))

Note that eval() is highly insecure. For this program maybe it doesn't 
matter but in general you should avoid using eval().

> 
> frame = Frame(None)
> 
> entry = Entry(frame)
> entry['textvariable'] = expression = StringVar()
> entry.bind("", evaluate)

You need to say what kind of event you want to bind to the evaluate 
function. In this case you probably want to bind the Enter key so you 
would use
   entry.bind("<Return>", evaluate)

Kent

> 
> label = Label(frame)
> 
> button = Button(frame, text = "Submit", command = evaluate)
> 
> frame.pack()
> entry.pack()
> label.pack()
> button.pack()
> frame.mainloop()
> 
> I get this:
> 
>>>> Traceback (most recent call last):
>   File
> "D:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "D:\Python24\Calculator\Calc.py", line 12, in ?
>     entry.bind("", evaluate)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 933, in bind
>     return self._bind(('bind', self._w), sequence, func, add)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 888, in _bind
>     self.tk.call(what + (sequence, cmd))
> TclError: no events specified in binding
> 
> Please have a look for me thanks!
> 
> 
> 
> 
> Joe Cox
> 513-293-4830
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list