summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2000-02-25 21:54:19 (GMT)
committerBarry Warsaw <barry@python.org>2000-02-25 21:54:19 (GMT)
commit7d3f27c090038d7fba27f5da8e88329ff91b32d9 (patch)
treeb9674a578f73280cdc98955714edbe6b8d59a66b
parent02a1c40051e4d4ad081c38a368fd87da89ede5ac (diff)
downloadcpython-7d3f27c090038d7fba27f5da8e88329ff91b32d9.zip
cpython-7d3f27c090038d7fba27f5da8e88329ff91b32d9.tar.gz
cpython-7d3f27c090038d7fba27f5da8e88329ff91b32d9.tar.bz2
Changes inspired by Randall Hooper to allow callbacks when an
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.
-rw-r--r--Lib/lib-tk/Tkinter.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py
index 30cc982..a95bf42 100644
--- a/Lib/lib-tk/Tkinter.py
+++ b/Lib/lib-tk/Tkinter.py
@@ -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):