[Python-checkins] CVS: python/dist/src/Lib/lib-tk Tkinter.py,1.134,1.135

Barry A. Warsaw bwarsaw@cnri.reston.va.us
Fri, 25 Feb 2000 16:54:21 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Lib/lib-tk
In directory anthem:/home/bwarsaw/projects/python/Lib/lib-tk

Modified Files:
	Tkinter.py 
Log Message:
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.


Index: Tkinter.py
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Lib/lib-tk/Tkinter.py,v
retrieving revision 1.134
retrieving revision 1.135
diff -C2 -r1.134 -r1.135
*** Tkinter.py	1999/10/20 12:29:56	1.134
--- Tkinter.py	2000/02/25 21:54:19	1.135
***************
*** 1,5 ****
  # Tkinter.py -- Tk/Tcl widget wrappers
  
! __version__ = "$Revision: 1.134 $"
  
  import sys
--- 1,5 ----
  # Tkinter.py -- Tk/Tcl widget wrappers
  
! __version__ = "$Revision: 1.135 $"
  
  import sys
***************
*** 1791,1802 ****
  
  class _setit:
! 	def __init__(self, var, value):
  		self.__value = value
  		self.__var = var
  	def __call__(self, *args):
  		self.__var.set(self.__value)
  
  class OptionMenu(Menubutton):
! 	def __init__(self, master, variable, value, *values):
  		kw = {"borderwidth": 2, "textvariable": variable,
  		      "indicatoron": 1, "relief": RAISED, "anchor": "c",
--- 1791,1805 ----
  
  class _setit:
! 	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, **kwargs):
  		kw = {"borderwidth": 2, "textvariable": variable,
  		      "indicatoron": 1, "relief": RAISED, "anchor": "c",
***************
*** 1806,1812 ****
  		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
  
--- 1809,1823 ----
  		menu = self.__menu = Menu(self, name="menu", tearoff=0)
  		self.menuname = menu._w
! 		# '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, callback))
  		self["menu"] = menu