Tkinter Event Types

C

codecraig

Hi,
When I do something like.

s = Scale(master)
s.bind("<ENTER>", callback)

def callback(self, event):
print event.type

I see "7" printed out. Where are these constants defined for various
event types? Basically i want to do something like...

def callback(self, event):
if event.type == ENTER:
print "You entered"

thanks
 
T

tiissa

codecraig said:
Hi,
When I do something like.

s = Scale(master)
s.bind("<ENTER>", callback)

def callback(self, event):
print event.type

I see "7" printed out. Where are these constants defined for various
event types? Basically i want to do something like...
7 must be for KeyPressed.
That's the type of the event (among Activate, Button, KeyPressed...).
def callback(self, event):
if event.type == ENTER:
print "You entered"

You may want to look at event.keycode (36 for Return).
 
J

Jeff Epler

The "type" field is related to the definition of different events in
X11. In Xlib, the event structure is a "C" union with the first
(common) field giving the type of the event so that the event-dependant
fields can be accessed through the proper union member.

Generally, you won't use this field in Tkinter programs.

jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCZFtQJd01MZaTXX0RAmffAJwPXtWUugiSVGukuqc27MZWcrs2zACfZfH7
PRNDFDXbj+DH9Df2xQywnWg=
=2ryT
-----END PGP SIGNATURE-----
 
E

Eric Brunel

Hi,
When I do something like.

s = Scale(master)
s.bind("<ENTER>", callback)

def callback(self, event):
print event.type

I see "7" printed out. Where are these constants defined for various
event types? Basically i want to do something like...

def callback(self, event):
if event.type == ENTER:
print "You entered"

The usual way is to bind different callbacks on different events. So you won't have to test the event type in your callback, since the callback itself will already have been selected from the event type. If you insist in doing it the way you mention, you can always do:


ENTER_EVENT = 1
KEYPRESS_EVENT = 2
# ...

def myCallback(evtType, evt):
if evtType == ENTER_EVENT:
# some action...
elif evtType == KEYPRESS_EVENT:
# some other action...
# ...

myWidget.bind('<Enter>', lambda evt: myCallback(ENTER_EVENT, evt))
myWidget.bind('<KeyPress>', lambda evt: myCallback(KEYPRESS_EVENT, evt))


I can't really see what it will be good for however...

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top