diff options
author | Guido van Rossum <guido@python.org> | 1996-10-21 15:16:51 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1996-10-21 15:16:51 (GMT) |
commit | 28574b557b40b8ebc964e0b5f27da42f9b4aa611 (patch) | |
tree | 37f1f232f9ef3b80b2e251750fdc405b4be5a02a /Lib | |
parent | cc55c2d295cbfadcb2f9799679fc228c81fd17ab (diff) | |
download | cpython-28574b557b40b8ebc964e0b5f27da42f9b4aa611.zip cpython-28574b557b40b8ebc964e0b5f27da42f9b4aa611.tar.gz cpython-28574b557b40b8ebc964e0b5f27da42f9b4aa611.tar.bz2 |
(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.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/lib-tk/Tkinter.py | 33 | ||||
-rwxr-xr-x | Lib/tkinter/Tkinter.py | 33 |
2 files changed, 56 insertions, 10 deletions
diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py index 13886eb..3a7b714 100644 --- a/Lib/lib-tk/Tkinter.py +++ b/Lib/lib-tk/Tkinter.py @@ -1522,13 +1522,36 @@ class Text(Widget): def yview_pickplace(self, *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): + kw = {"borderwidth": 2, "textvariable": variable, + "indicatoron": 1, "relief": RAISED, "anchor": "c", + "highlightthickness": 2} + Widget.__init__(self, master, "menubutton", kw) self.widgetName = 'tk_optionMenu' - Widget._setup(self, master, {}) - self.menuname = apply( - self.tk.call, - (self.widgetName, self._w, variable, value) + values) + menu = self.__menu = Menu(self, name="menu", tearoff=0) + self.menuname = menu._w + menu.add_command(label=value, command=_setit(variable, value)) + 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: def __init__(self, imgtype, name=None, cnf={}, **kw): diff --git a/Lib/tkinter/Tkinter.py b/Lib/tkinter/Tkinter.py index 13886eb..3a7b714 100755 --- a/Lib/tkinter/Tkinter.py +++ b/Lib/tkinter/Tkinter.py @@ -1522,13 +1522,36 @@ class Text(Widget): def yview_pickplace(self, *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): + kw = {"borderwidth": 2, "textvariable": variable, + "indicatoron": 1, "relief": RAISED, "anchor": "c", + "highlightthickness": 2} + Widget.__init__(self, master, "menubutton", kw) self.widgetName = 'tk_optionMenu' - Widget._setup(self, master, {}) - self.menuname = apply( - self.tk.call, - (self.widgetName, self._w, variable, value) + values) + menu = self.__menu = Menu(self, name="menu", tearoff=0) + self.menuname = menu._w + menu.add_command(label=value, command=_setit(variable, value)) + 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: def __init__(self, imgtype, name=None, cnf={}, **kw): |