Tkinter.event.widget: handler gets name instead of widget.



Hi widget wizards,

The manual describes the "event" attribute "widget" as "The widget
which generated this event. This is a valid Tkinter widget instance, not
a name. This attribute is set for all events."
Ans so it is--has been until on the latest occasion "event.widget" was
not the widget, but its name, crashing the handler.
# Here I build a list of selectable records having each four fields.
# The fields are labels. The selectable line is a frame containing the
# labels side by side. The line frames go into self, which is a Frame.

for n in range (len (records)):
record = records [n]
line_frame = Frame (self, name = '-%d-' % n, relief = RAISED, **BUTTON_FRAME_)
line_frame.bind ('<Enter>', self.enter)
line_frame.bind ('<Leave>', self.leave)
line_frame.bind ('<ButtonRelease-1>', self.pick_record)
line_frame.bind ('<ButtonRelease-3>', self.info_profile)
line_frame.grid (row = n+2, column = 1)
for i in self.range_n_fields: # (0, 1, 2, 3)
field = Label (line_frame, text = record [self.INDICES [i]], anchor = W, width = self.COLUMN_WIDTHS [i], **DB_LIST_LABEL_)
field.grid (row = 0, column = i, sticky = NW)

# Here is the <Enter> handler:

def enter (self, event):
w = event.widget
print 'hit list.enter (). Event, widget', event, w, w.__class__ # Tracing line
w.config (bg = SELECTED_BG_COLOR)

# And here is what comes out. The first line is my tracing line. The name is correct in that it
# names the entered line_frame, but is wrong because it should be the line_frame, not its name.
# The rest is the red exception message:

hit list.enter (). Event, widget <Tkinter.Event instance at 0x9115dcc> .main-frame.data-frame.title-hit-list.-10- <type 'str'>
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "/home/fr/python/finance/piam/hit_list.py", line 83, in enter
w.config (bg = SELECTABLE_BG_COLOR)
AttributeError: 'str' object has no attribute 'config'

# The same thing happens with <Leave>. The other handlers I haven't done yet. The same bindings work well in
# a Menu class with the difference that the bindings are on the Labels, not a containing Frame.

# Dell E6500, Ubuntu 10.04, Python 2.6


Frederic


.



Relevant Pages

  • GridBagLayout and its pitfals
    ... related to fills and paddings and reorganize things. ... several text fields with labels, several checkboxes, ... filling the entire frame, but a mix of various GUI ... trying to make one GUI element ...
    (comp.lang.java.programmer)
  • GridBagLayout and its pitfals
    ... Nested GridBagLayout and its pitfals. ... several text fields with labels, several checkboxes, ... filling the entire frame, but a mix of various GUI ... Create separate panels for each group of related GUI elements ...
    (comp.lang.java.programmer)
  • Re: Some label controls appear black on some computers
    ... The Label controls are directly on the Form1 and there is no Frame control ... I noticed that the 3 Label controls which show a black color have an Opaque ... All other labels which appear normal have Transparent BackStyle. ... put a picturebox in the frame (must be ...
    (microsoft.public.vb.general.discussion)
  • Re: Some label controls appear black on some computers
    ... The Label controls are directly on the Form1 and there is no Frame control ... All other labels which appear normal have Transparent ... put a picturebox in the frame (must be ...
    (microsoft.public.vb.general.discussion)
  • Re: Some label controls appear black on some computers
    ... The Label controls are directly on the Form1 and there is no Frame control ... I noticed that the 3 Label controls which show a black color have an Opaque ... All other labels which appear normal have Transparent BackStyle. ... Are you using a manifest file on this particular WinXP box? ...
    (microsoft.public.vb.general.discussion)