tkinter 3.0 multiple keyboard events together

Pavel Kosina geon at post.cz
Sat Dec 27 09:08:24 EST 2008


janislaw napsal(a):
> Use google to find the appropriate site, or browse this site, there
> are plenty of examples. You may want to examine the code I wrote to
> you to catch the idea:
> #----------------------
> import Tkinter
> import pprint
>
> tk = Tkinter.Tk()
> f = Tkinter.Frame(tk, width=100, height=100)
> msg = Tkinter.StringVar()
> msg.set('Hello')
> l = Tkinter.Label(f,  textvariable=msg)
> l.pack()
> f.pack()
>
> keys = set()
>
> def keyPressHandler(event):
>     keys.add(event.char)
>     display()
>
> def keyReleaseHandler(event):
>     keys.remove(event.char)
>     display()
>
> def display():
>     msg.set(str(keys))
>
> f.bind_all('<KeyPress>', keyPressHandler)
> f.bind_all('<KeyRelease>', keyReleaseHandler)
>
> f.mainloop()
>   

Is this really the most simple solution how to do this in Tkinter? Is 
there nothing cleaner "build inside"?

This solution has disadvantage that after you release one key, that the 
function keyPressHandler stopped to be called by the other pressed keys. 
I would have not to build moving the player in KeyPresshandler, but on 
another new function that would have to read periodically "keys" and to 
act afterwards....

I really tried to search for words mentioned in this subject, but 
nothing, not even this solution, was found by me.

PK





More information about the Python-list mailing list