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

Frederic Rentsch anthra.norell at bluewin.ch
Mon Jul 9 16:39:45 EDT 2012


On Mon, 2012-07-09 at 01:58 -0400, Terry Reedy wrote:
> On 7/8/2012 5:19 PM, Frederic Rentsch wrote:
> > 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."
> 
> Same in 3.3, with nice example of using it.
> 
> def turnRed(self, event):
>      event.widget["activeforeground"] = "red"
> 
> self.button.bind("<Enter>", self.turnRed)
> 
> > 	Ans so it is--has been until on the latest occasion "event.widget" was
> > not the widget, but its name, crashing the handler.
> 
> Has event.widget been the widget only in other programs or previously
> with the same program?

I bind <Enter> to Frames, each Frame calling the same handler that is
supposed to change the background color. It is the Enter action that
generates the event. No later the handler receives the event whose
attribute widget is the widget's name (full path). My code doesn't
create events anywhere. I suppose events vanish when the last handler
terminates.

. . .

> When posting problem code, you should post a minimal, self-contained 
> example that people can try on other systems and versions. 

Attempting to strip the critical code, throwing out everything
incidental to the problem so I could post something intelligible, I
failed to fail: the bare essentials work. The problem appears to be in
the incidental.

> Can you 
> create the problem with one record, which you could give, and one 
> binding? Do you need 4 fields, or would 1 'work'?
> 
It fails even with the Frame containing no Labels at all, like this:

for n, record in enumerate(records):
	line_frame = Frame (self, name = _verbalize_number (n), width = 600, height = 20, relief = RAISED, **BUTTON_FRAME_)
	line_frame.bind ('<Enter>', self.enter)
## No Labels at all:
##	for i in self.range_n_fields:
##		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)

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

hit list.leave (). Event, widget <Tkinter.Event instance at 0xa52c60c> .main-frame.data-frame.title-hit-list.one-zero <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 114, in enter
    w.config (bg = ENTERED_BG_COLOR)
AttributeError: 'str' object has no attribute 'config'

_verbalize_number spells out the line numbers, because the manual says
something about digits being reserved for the auto-generated widget
names. I thought that assigned names containing digits might be a
problem, but it wasn't.
   The dictionary arguments, by the way, are just style elements:
colors, fonts, reliefs, etc. nothing functionally essential.

> >
> > # Dell E6500, Ubuntu 10.04, Python 2.6
> 
> You might try a current Python release, and the latest tcl/tk 8.5.11 
> released last March (comes with 3.3.0 Windows release, don't know how on 
> Ubuntu). There might be (have been?) a bug with events on Frames, or on 
> Frames within Frames treated as widgets.
> 
> -- 
> Terry Jan Reedy
> 

Terry,

I interspersed a couple of answers above. As to your last suggestion I
got Python 2.7.3 and managed to compile it. I would have preferred
something ready to install, but that doesn't seem to be available for
Linux. The compile went smoothly. But it'll take me another day to
reorganize, beginning with the Applications menu which still shows "IDLE
(Python 2.6)", while terminals already call the new version 2.7.3, but
it doesn't know where MySQLdb is, and possibly where other things are.
So for now I can't report on this effort either.

But I certainly appreciate your help. Many thanks.


Frederic





More information about the Python-list mailing list