Need help with Tkinter bind statement

John Grayson johngrayson at home.com
Tue Mar 6 18:23:32 EST 2001


-----Original Message-----


>  > Hello -
>  > I have been working on an RPN calculator program for a while now, and
>  > I am having problems with some bindings.  Here is a code snippet of
>  > where I'm having trouble:


Snip..


>  > The second problem is that when I
>  > use any of these keys, the keystroke shows up in the entry widget.
>  > What I want to happen is that pressing the "+" key, for example,
>  > should add the values in the xStack and yStack (which it does) and
>  > then put the result in the yStack entry widget (which it also does).
>  > The problem is that the "+" is left in the xStack.  The contents of
>  > the xStack are deleted by the Calc function, so I thought it would
>  > also delete the "+" as well.

>  > Thanks.

>  > Mike

Here is the solution to both your problems...

from Tkinter import *

root = Tk()

def gotit(which):
    print 'Got', which
    return "break"

frame = Frame(root)
entry = Entry(frame)
entry.pack(anchor=CENTER)
frame.pack()

entry.bind('<+>', lambda event, fxn = 'y+x':gotit(fxn))
entry.bind('<Key-minus>', lambda event, fxn = 'y-x':gotit(fxn))
entry.bind('<*>', lambda event, fxn = 'y*x':gotit(fxn))
entry.bind('</>', lambda event, fxn = 'y/x':gotit(fxn))

root.mainloop()


1) You've got to use <Key-minus> since "-" is a valid token
   in the bind...

2) return "break" stops the propagagation of the event to
   the widget, so it does not insert the character.



     John Grayson








More information about the Python-list mailing list