[Tutor] disabling Pmw ComboBox's EntryField? [quick lambda hacks]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 15 Feb 2002 13:28:43 -0800 (PST)


On Fri, 15 Feb 2002 lsloan@umich.edu wrote:

> 
> How do I disable a Pmw ComboBox's EntryField and make its dropdown menu
> open when that EntryField is clicked?
> 
> What I've tried in my class so far is:
> 
> 	self.cboxDomain = Pmw.ComboBox(grpNav.interior(),
> 		label_text='Domain:',
> 		labelpos='w',
> 		fliparrow=1,
> 		selectioncommand=self.clickDomain)
> 	self.cboxDomain.component('entryfield_entry').bind('<Button-1>',
> 		self.cboxDomain.invoke)

I haven't played with Python megawidgets yet, but the error message here:

> 	  Args: (<Tkinter.Event instance at 578744>,)
> 	  Event type: ButtonPress (type num: 4)
> 	Traceback (innermost last):
> 	  File ".../Pmw/Pmw_0_8_5/lib/PmwBase.py", line 1690, in
> 	  __call__
> 		None
> 	TypeError: too many arguments; expected 1, got 2


implies that self.cboxDomain.invoke() doesn't need to take the 'event'
parameter that get's passed when we bind it to Button-1.  We can do a
quicky thing to allow it to ignore the event parameter:


###
self.cboxDomain.component('entryfield_entry').bind('<Button-1>',
                          lambda event: self.cboxDomain.invoke())
###

The lambda part just makes a callback function that discards any 'event'
aimed at it.



The above fix may not work perfectly yet.  If you're using a Python older
than 2.2, you may need to do something to simulate lexical sope, perhaps
something like this:

###
self.cboxDomain.component('entryfield_entry').bind('<Button-1>',
                          lambda event,
                                 self=self: self.cboxDomain.invoke())
###

because we really need the callback to know about it's 'self' to be able
to get cboxDomain() to invoke().


If you have any questions, please feel free to ask.  Hope this helps!