tkinter 3.0 multiple keyboard events together

janislaw wicijowski at gmail.com
Sat Dec 27 15:51:37 EST 2008


On 27 Gru, 15:08, Pavel Kosina <g... at post.cz> wrote:
> 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"?

Um, I could be only guessing what are you meant to do, unless you
describe your problem in more detailed way. I.e. describe the desired
behaviour, show code which you have, and describe the current
behaviour.

> 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....

Hmmm. Maybe you'd like to hook into Tkinter event loop, i.e. by custom
events if you don't like periodical polling.

>From what I am guessing, you expect that 2 keyboard events are
simultaneous and can be cached at the same time instant. Well that
would be impossible, cause all the underlying hardware is sequential.
You may have parallel stuff if you go down to VHDL and write your own
hardware, but when you have a CPU, then you'll have to stick with
writing programs. C'mon, 20 lines is not such a big deal.

Regards
JW



More information about the Python-list mailing list