On http://www.sunworld.com/swol-02-1998/swol-02-python.html I saw
this nice litte python script:
---------------------------------------------------------------------------------------------------------------
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()
---------------------------------------------------------------------------------------------------------------
unfortunately following error occured:
Traceback (innermost last):
File "C:\Programme\Python\Tools\Scripts\calc.py", line 11, in ?
entry.bind("", evaluate)
File "C:\Programme\Python\Lib\lib-tk\Tkinter.py", line 473, in bind
return self._bind(('bind', self._w), sequence, func, add)
File "C:\Programme\Python\Lib\lib-tk\Tkinter.py", line 466, in _bind
apply(self.tk.call, what + (sequence, cmd))
TclError: no events specified in binding
Since I am new to python, I can't figure out how to correct the code
above. Any takers?
Thanks a lot in advance!
bye
Chris...
> Hello...
>
> On http://www.sunworld.com/swol-02-1998/swol-02-python.html I saw
> this nice litte python script:
>
[Much clipped...]
> entry.bind("", evaluate)
[More clippage]
> TclError: no events specified in binding
>
> Since I am new to python, I can't figure out how to correct the code
> above. Any takers?
The entry.bind() line needs to have an event type to bind to. Changing this line to:
entry.bind("<KeyRelease-Return>", evaluate)
will fix it (I tried), and will perform evaluation of the expression when the return
key is pressed. Note that the whole program is kind of a kludge (try "4 +" as input,
for example); a good learning exercise might be to improve it a bit... Perhaps wrap
the eval() in a try/except pair and capture bad expressions. Then you could also
change:
entry.bind("<KeyRelease-Return>", evaluate)
to:
entry.bind("<KeyRelease>", evaluate)
And have the result field update dynamically with each keypress (rather than having
to explicitly press return)
Hope that helps,
Chad Netzer
ch...@vision.arc.nasa.gov