Event Problem in Tkinter

Bob Parnes rparnes at megalink.net
Thu Oct 11 10:05:44 EDT 2001


Hi,

I am getting a false result in using events in Tkinter to identify the 
particular widget that a user selects to enter text. I submitted the 
problem to John Grayson's online help at www.manning.com and got no reply, 
so I am trying this newsgroup as a last resort.

As a test on a linux computer, I prepared the following code to create a 
2x2 array of entry widgets. The code is freely adapted from Example_6_2.py 
in _Python and Tkinter Programming_

------------------------------
#!/usr/bin/env python2

from Tkinter import *

eventList = {'2': 'KeyPress', '4': 'ButtonPress', \
            '9': 'FocusIn'}

root = Tk()

def reportEvent(event):
    if event.type in eventList.keys():
        x = event.x_root
        y = event.y_root
        print 'Event: %s, coordinates: %d, %d; widget: %s' \
            % (eventList[event.type], x, y, event.widget)
        if event.type == '9': print '-'*75
    
fm = Frame(root, border=2)
text = []
for n in range(4):
    text.append(Entry(fm, width=10))
    for event in eventList.values():
        text[n].bind('<%s>' % event, reportEvent)
for n,r,c in ((0,0,0), (1,0,1), (2,1,0), (3,1,1)):
    text[n].grid(row=r, column=c)
fm.pack(padx=8, pady=8)

print '\nList of Entry Widgets'
for item in fm.winfo_children():
    if item.winfo_class() == 'Entry':
        print item

root.mainloop()
-----------------------------

If I run the program and use the mouse, for example, to enter the top left 
entry widget, then tab to the one on its right, I get the followingresult:

List of Entry Widgets
.135346692.135404836
.135346692.135400852
.135346692.135529924
.135346692.135546500
Event: ButtonPress, coordinates: 47, 546; widget: .135346692.135404836
Event: FocusIn, coordinates: 1076693612, 135248272; widget: None
---------------------------------------------------------------------------
Event: KeyPress, coordinates: 190, 679; widget: None
Event: FocusIn, coordinates: 1076693612, 135248272; widget: None
---------------------------------------------------------------------------

Apparently the ButtonPress event identifies the correct widget, but the 
FocusIn and KeyPress events do not. Although a user could use only a mouse 
to move among widgets, I would prefer a tabbing capability as well.

Am I missing something? If not, is there an alternate approach? Thanks for 
any help.

-- 
Bob Parnes
rparnes at megalink.net



More information about the Python-list mailing list