]> granicus.if.org Git - python/commitdiff
Changes inspired by Randall Hooper to allow callbacks when an
authorBarry Warsaw <barry@python.org>
Fri, 25 Feb 2000 21:54:19 +0000 (21:54 +0000)
committerBarry Warsaw <barry@python.org>
Fri, 25 Feb 2000 21:54:19 +0000 (21:54 +0000)
OptionMenu is modified.  Somewhat rewritten and elaborated by myself.

class _setit: The constructor now takes an optional argument
`callback' and stashes this in a private variable.  If set, the
__call__() method will invoke this callback after the variable's value
has changed.  It will pass the callback the value, followed by any
args passed to __call__().

class OptionMenu: The constructor now takes keyword arguments, the
only one that's legally recognized is `command', which can be set to a
callback.  This callback is invoked when the OptionMenu value is set.
Any other keyword argument throws a TclError.

Lib/lib-tk/Tkinter.py

index 30cc982552cb003d65dd63a3773a09d267708535..a95bf42b56e4d20bce9fbc438a669aae739f86df 100644 (file)
@@ -1790,14 +1790,17 @@ class Text(Widget):
                self.tk.call((self._w, 'yview', '-pickplace') + what)
 
 class _setit:
-       def __init__(self, var, value):
+       def __init__(self, var, value, callback=None):
                self.__value = value
                self.__var = var
+               self.__callback = callback
        def __call__(self, *args):
                self.__var.set(self.__value)
+               if self.__callback:
+                       apply(self.__callback, (self.__value,)+args)
 
 class OptionMenu(Menubutton):
-       def __init__(self, master, variable, value, *values):
+       def __init__(self, master, variable, value, *values, **kwargs):
                kw = {"borderwidth": 2, "textvariable": variable,
                      "indicatoron": 1, "relief": RAISED, "anchor": "c",
                      "highlightthickness": 2}
@@ -1805,9 +1808,17 @@ class OptionMenu(Menubutton):
                self.widgetName = 'tk_optionMenu'
                menu = self.__menu = Menu(self, name="menu", tearoff=0)
                self.menuname = menu._w
-               menu.add_command(label=value, command=_setit(variable, value))
+               # 'command' is the only supported keyword
+               callback = kwargs.get('command')
+               if kwargs.has_key('command')
+                       del kwargs['command']
+               if kwargs:
+                       raise TclError, 'unknown option -'+kwargs.keys()[0]
+               menu.add_command(label=value,
+                                command=_setit(variable, value, callback))
                for v in values:
-                       menu.add_command(label=v, command=_setit(variable, v))
+                       menu.add_command(label=v,
+                                        command=_setit(variable, v, callback))
                self["menu"] = menu
 
        def __getitem__(self, name):