Skip to content

Commit ce056aa

Browse files
authored
Create load.md
1 parent 7184b9f commit ce056aa

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

test/load.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
tkinter 3.0 multiple keyboard events together
2+
janislaw wicijowski at gmail.com
3+
Sun Dec 28 21:34:04 CET 2008
4+
Previous message (by thread): tkinter 3.0 multiple keyboard events together
5+
Next message (by thread): tkinter 3.0 multiple keyboard events together
6+
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
7+
On 28 Gru, 09:43, Pavel Kosina <g... at post.cz> wrote:
8+
> well, I am working on a tutorial for youngster (thats why i need to stay
9+
> the code as easy as possible). In this game you are hunted by robots. I
10+
> could use key"7" on numeric keypad for left-up moving but seems to me,
11+
> that "4"+"8" is much more standard for them.
12+
>
13+
> > Hmmm. Maybe you'd like to hook into Tkinter event loop, i.e. by custom
14+
> > events if you don't like periodical polling.
15+
>
16+
> No, that would be even more difficult. I already have a code that use
17+
> your idea:
18+
>
19+
> from Tkinter import *
20+
> root = Tk()
21+
>
22+
> pressedKeys=set()
23+
>
24+
> def onKeyPress(event):
25+
> pressedKeys.add(event.keysym)
26+
>
27+
> def onKeyRelease(event):
28+
> pressedKeys.remove(event.keysym)
29+
>
30+
> def move():
31+
> print list(pressedKeys)
32+
> root.after(100,move)
33+
>
34+
> root.bind("<KeyPress>", onKeyPress)
35+
> root.bind("<KeyRelease>", onKeyRelease)
36+
> root.after(100,move)
37+
> root.mainloop()
38+
>
39+
> well, I thought that gui´s have such a problem already built-in - so
40+
> that i am not pressed to code it. You know, its not exactly about me -
41+
> if I do it for myself, for my program, that there is no problem, but I
42+
> need to explained it to begginners ..... And I do not want, as might be
43+
> more usual, do my module, that would cover this "insanity" (look at it
44+
> with 13-years old boy eyes ;-) ). Do you like to say me, that this is
45+
> not a standard "function" neither in tkinter, not say gtk or the others,
46+
> too?
47+
48+
Ok, I get it then! Your goal is very humble :)
49+
50+
In the piece of code we have came together to, there is a
51+
functionality you already want - the pressedKeys. I suppose, that
52+
you'd like to hide from the further implementors (children). You can
53+
write another module, which could contain this piece of code and could
54+
be imported by kids to have the desired features.
55+
56+
Meaning only getting this list of keys.
57+
58+
But what you write here:
59+
60+
> I would expect something like this:
61+
>
62+
> def onKeyTouch(event):
63+
> print (event.keysymAll)
64+
>
65+
> root.bind("<KeyPress>", onKeyTouch)
66+
67+
...is a bit misleading. There is no such thing as simultaneous events
68+
- for that the event dispatcher loop is designed - to simplyfy all
69+
usual
70+
stuff with parallel paradigsm such as interrupts, thread safety
71+
etc. An event is in my eyes a response to a short action, let's call
72+
it
73+
atomic. There's no need to pretend, that clicking a few keys
74+
simultanously is sth immediate and the software could by any chance
75+
guess that a few keys were actually clicked. Even the keyboard driver
76+
does some sequential scanning on the keys, and the key codes are then
77+
send to the board sequentially as well. Try to run this code:
78+
79+
#----------------
80+
#Tkinter multiple keypress
81+
#Answer http://groups.google.com/group/comp.lang.python/browse_thread/thread/88788c3b0b0d51d1/c4b7efe2d71bda93
82+
import Tkinter
83+
import pprint
84+
import time
85+
86+
tk = Tkinter.Tk()
87+
f = Tkinter.Frame(tk, width=100, height=100)
88+
msg = Tkinter.StringVar()
89+
msg.set('Hello')
90+
l = Tkinter.Label(f, textvariable=msg)
91+
l.pack()
92+
f.pack()
93+
94+
keys = set()
95+
96+
lastTime = None
97+
98+
def keyPressHandler(event):
99+
keys.add(event.char)
100+
global lastTime
101+
now = time.time()
102+
if lastTime:
103+
difference = now - lastTime
104+
if difference < 0.1: #seconds
105+
print difference, 's'
106+
lastTime = now
107+
display()
108+
109+
def keyReleaseHandler(event):
110+
keys.remove(event.char)
111+
display()
112+
113+
def display():
114+
msg.set(str(keys))
115+
116+
f.bind_all('<KeyPress>', keyPressHandler)
117+
f.bind_all('<KeyRelease>', keyReleaseHandler)
118+
119+
f.mainloop()
120+
#--------------------------------
121+
122+
123+
... and find out, how 'simultaneous' they are. For me it's 0.0,
124+
meaning that the event handlers were executed one after another, but
125+
sometimes there is sth like 0.0309998989105 s - proabably an USB
126+
latency, or an operating system context switch - hard to tell.
127+
128+
If you'd like to have two key clicks to generate a single event, you'd
129+
have to write some timeout code (threading.Timer or tk stuff is handy)
130+
to check how 'simultaneous' they actually were. Search web for events
131+
in <<doubled>> inequality signs if you really want this.
132+
133+
I don't know gtk, but hey, pygame has pygame.key.get_pressed if you'd
134+
like to switch:
135+
136+
http://www.pygame.org/docs/ref/key.html
137+
138+
Have luck
139+
Jan
140+
141+
Previous message (by thread): tkinter 3.0 multiple keyboard events together
142+
Next message (by thread): tkinter 3.0 multiple keyboard events together
143+
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
144+
More information about the Python-list mailing list

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy