Disabling Comboboxes
Richard Chamberlain
richard_chamberlainNOriSPAM at ntlworld.com.invalid
Fri Jun 23 10:13:15 EDT 2000
Hi Kapil,
In the ToDo list for Pmw disabling is mentioned so hopefully
we'll have a solution from the source before long. In the
meantime...
This is a bit of hack (in the derogatory sense of the word) but
hey!
So you basically want to import the myComboBox class and create
an instance just as you would with Pmw. I've added a couple of
methods - disable and enable suprisingly enough.
Included below is some test code.
It works in the same way as you mentioned, by unbinding the
events, and rebinding them to enable it.
Richard
from Tkinter import *
import Pmw
root = Tk()
root.title('Disabling ComboBox Hack')
Pmw.initialise()
class myComboBox(Pmw.ComboBox):
def disable(self):
# Rebind things to my empty handler
self.component('arrowbutton').bind('<1>',self.handler)
self.component('arrowbutton').bind('<3>',self.handler)
self.component('arrowbutton').bind('<Shift-
3>',self.handler)
self.component('entryfield_entry').configure
(state='disabled',fg='grey')
def enable(self):
# bind the events back up to the original methods
self.component('arrowbutton').bind('<1>',self._postList)
self.component('arrowbutton').bind('<3>',self._next)
self.component('arrowbutton').bind('<Shift-3>',
self._previous)
self.component('arrowbutton').configure(takefocus=1)
self.component('entryfield_entry').configure
(state='normal',fg='black')
def handler(self,event):
# so it doesn't propagate the event
return('break')
# Now test it
comboentries = ("Pmw Should", "have a nicer way", "to
disable", "a combo box")
theEnabler = Button(root, text='Enabled',padx=20, pady=10)
theEnabler.pack(expand=1, fill=BOTH, padx=8, pady=8)
combobox = myComboBox(root, label_text='Disabling Combobox:',
labelpos='wn',
listbox_width=24, dropdown=1,
scrolledlist_items=comboentries)
combobox.pack(fill=BOTH, expand=1, padx=8, pady=8)
combobox.selectitem(comboentries[0])
def disable(event):
if theEnabler['text']=='Enabled':
combobox.disable()
theEnabler.configure(text='Disabled')
else:
combobox.enable()
theEnabler.configure(text='Enabled')
theEnabler.bind('<Button-1>',disable)
root.mainloop()
Got questions? Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com
More information about the Python-list
mailing list