(Fred Drake:) Re-wrote the OptionMenu class to allow access to a real

Menu object via om['menu'] -- this is necessary to use a post-command
with an OptionMenu.  The API has not changed.
This commit is contained in:
Guido van Rossum 1996-10-21 15:16:51 +00:00
parent cc55c2d295
commit 28574b557b
2 changed files with 56 additions and 10 deletions

View File

@ -1522,13 +1522,36 @@ class Text(Widget):
def yview_pickplace(self, *what): def yview_pickplace(self, *what):
apply(self.tk.call, (self._w, 'yview', '-pickplace')+what) apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
class OptionMenu(Widget): class _setit:
def __init__(self, var, value):
self.__value = value
self.__var = var
def __call__(self, *args):
self.__var.set(value)
class OptionMenu(Menubutton):
def __init__(self, master, variable, value, *values): def __init__(self, master, variable, value, *values):
kw = {"borderwidth": 2, "textvariable": variable,
"indicatoron": 1, "relief": RAISED, "anchor": "c",
"highlightthickness": 2}
Widget.__init__(self, master, "menubutton", kw)
self.widgetName = 'tk_optionMenu' self.widgetName = 'tk_optionMenu'
Widget._setup(self, master, {}) menu = self.__menu = Menu(self, name="menu", tearoff=0)
self.menuname = apply( self.menuname = menu._w
self.tk.call, menu.add_command(label=value, command=_setit(variable, value))
(self.widgetName, self._w, variable, value) + values) for v in values:
menu.add_command(label=v, command=_setit(variable, v))
self["menu"] = menu
def __getitem__(self, name):
if name == 'menu':
return self.__menu
return Widget.__getitem__(self, name)
def destroy(self):
Menubutton.destroy(self)
self.__menu = None
class Image: class Image:
def __init__(self, imgtype, name=None, cnf={}, **kw): def __init__(self, imgtype, name=None, cnf={}, **kw):

View File

@ -1522,13 +1522,36 @@ class Text(Widget):
def yview_pickplace(self, *what): def yview_pickplace(self, *what):
apply(self.tk.call, (self._w, 'yview', '-pickplace')+what) apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
class OptionMenu(Widget): class _setit:
def __init__(self, var, value):
self.__value = value
self.__var = var
def __call__(self, *args):
self.__var.set(value)
class OptionMenu(Menubutton):
def __init__(self, master, variable, value, *values): def __init__(self, master, variable, value, *values):
kw = {"borderwidth": 2, "textvariable": variable,
"indicatoron": 1, "relief": RAISED, "anchor": "c",
"highlightthickness": 2}
Widget.__init__(self, master, "menubutton", kw)
self.widgetName = 'tk_optionMenu' self.widgetName = 'tk_optionMenu'
Widget._setup(self, master, {}) menu = self.__menu = Menu(self, name="menu", tearoff=0)
self.menuname = apply( self.menuname = menu._w
self.tk.call, menu.add_command(label=value, command=_setit(variable, value))
(self.widgetName, self._w, variable, value) + values) for v in values:
menu.add_command(label=v, command=_setit(variable, v))
self["menu"] = menu
def __getitem__(self, name):
if name == 'menu':
return self.__menu
return Widget.__getitem__(self, name)
def destroy(self):
Menubutton.destroy(self)
self.__menu = None
class Image: class Image:
def __init__(self, imgtype, name=None, cnf={}, **kw): def __init__(self, imgtype, name=None, cnf={}, **kw):