Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25066

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Tkinter.event.widget: handler gets name instead of widget.
Date 2012-07-09 01:58 -0400
References <1341782353.2041.136.camel@hatchbox-one>
Newsgroups comp.lang.python
Message-ID <mailman.1933.1341813557.4697.python-list@python.org> (permalink)

Show all headers | View raw


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?


  	
> # 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)):

for n, record in enumerate(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)

When posting problem code, you should post a minimal, self-contained 
example that people can try on other systems and versions. Can you 
create the problem with one record, which you could give, and one 
binding? Do you need 4 fields, or would 1 'work'?


> # 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

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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Tkinter.event.widget: handler gets name instead of widget. Terry Reedy <tjreedy@udel.edu> - 2012-07-09 01:58 -0400
  Re: Tkinter.event.widget: handler gets name instead of widget. Rick Johnson <rantingrickjohnson@gmail.com> - 2012-07-09 10:49 -0700
    Re: Tkinter.event.widget: handler gets name instead of widget. Terry Reedy <tjreedy@udel.edu> - 2012-07-09 15:45 -0400
    Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch <anthra.norell@bluewin.ch> - 2012-07-09 22:47 +0200
    Re: Tkinter.event.widget: handler gets name instead of widget. Chris Angelico <rosuav@gmail.com> - 2012-07-10 08:23 +1000
    Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch <anthra.norell@bluewin.ch> - 2012-07-10 12:03 +0200
      Re: Tkinter.event.widget: handler gets name instead of widget. Rick Johnson <rantingrickjohnson@gmail.com> - 2012-07-10 15:11 -0700
        Re: Tkinter.event.widget: handler gets name instead of widget. Rick Johnson <rantingrickjohnson@gmail.com> - 2012-07-10 18:06 -0700
          Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch <anthra.norell@bluewin.ch> - 2012-07-11 09:59 +0200
        Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch <anthra.norell@bluewin.ch> - 2012-07-12 20:53 +0200
          Re: Tkinter.event.widget: handler gets name instead of widget. rantingrickjohnson@gmail.com - 2012-07-14 20:10 -0700
            Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch <anthra.norell@bluewin.ch> - 2012-07-16 12:32 +0200
          Re: Tkinter.event.widget: handler gets name instead of widget. rantingrickjohnson@gmail.com - 2012-07-14 20:10 -0700
        Re: Tkinter.event.widget: handler gets name instead of widget. Peter Otten <__peter__@web.de> - 2012-07-13 09:26 +0200
        Re: Tkinter.event.widget: handler gets name instead of widget. Frederic Rentsch <anthra.norell@bluewin.ch> - 2012-07-13 22:24 +0200
        Re: Tkinter.event.widget: handler gets name instead of widget. Terry Reedy <tjreedy@udel.edu> - 2012-07-13 19:23 -0400
        Re: Tkinter.event.widget: handler gets name instead of widget. Peter Otten <__peter__@web.de> - 2012-07-14 09:47 +0200

csiph-web