[Python-checkins] r63884 - in sandbox/trunk/ttk-gsoc/src: 2.x/ttk.py 3.x/ttk.py

guilherme.polo python-checkins at python.org
Mon Jun 2 03:57:38 CEST 2008


Author: guilherme.polo
Date: Mon Jun  2 03:57:37 2008
New Revision: 63884

Log:
First support class added: a themed OptionMenu, just like the OptionMenu already present in tkinter but this one uses ttk Menubutton instead

Modified:
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.py

Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py	Mon Jun  2 03:57:37 2008
@@ -19,7 +19,9 @@
 __all__ = ["Button", "Checkbutton", "Combobox", "Entry", "Frame",
            "Label", "Labelframe", "LabelFrame", "Menubutton", "Notebook",
            "Panedwindow", "PanedWindow", "Progressbar", "Radiobutton",
-           "Scrollbar", "Separator", "Sizegrip", "Style", "Treeview"]
+           "Scrollbar", "Separator", "Sizegrip", "Style", "Treeview",
+           # Extensions
+           "OptionMenu"]
 
 import Tkinter
 
@@ -1314,3 +1316,46 @@
     def yview(self, *args):
         """Query or modify vertical position of the treeview."""
         return self.tk.call(self._w, "yview", *args)
+
+
+# Extensions
+
+class OptionMenu(Menubutton):
+    """Themed OptionMenu which allows the user to select a value from a
+    menu."""
+
+    def __init__(self, master, variable, value, *values, **kwargs):
+        """Construct a themed OptionMenu widget with the parent master,
+        the resource textvariable set to variable, the initially selected
+        value specified by the value parameter, the other menu values
+        given by *values and an additional keyword argument command."""
+        kw = {'textvariable': variable, 'style': kwargs.pop('style', None)}
+        Menubutton.__init__(self, master, **kw)
+
+        menu = self.__menu = Tkinter.Menu(self, name="menu", tearoff=0)
+        self.menuname = menu._w
+
+        callback = kwargs.pop('command', None)
+        if kwargs:
+            raise Tkinter.TclError('unknown option -%s' % (
+                next(kwargs.iterkeys())))
+
+        menu.add_command(label=value,
+                         command=Tkinter._setit(variable, value, callback))
+        for v in values:
+            menu.add_command(label=v,
+                             command=Tkinter._setit(variable, v, callback))
+        self['menu'] = menu
+
+
+    def __getitem__(self, item):
+        if item == 'menu':
+            return self.__menu
+
+        return Menubutton.__getitem__(self, item)
+
+
+    def destroy(self):
+        """Destroy this widget and the associated menu."""
+        Menubutton.destroy(self)
+        self.__menu = None

Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py	Mon Jun  2 03:57:37 2008
@@ -19,7 +19,9 @@
 __all__ = ["Button", "Checkbutton", "Combobox", "Entry", "Frame",
            "Label", "Labelframe", "LabelFrame", "Menubutton", "Notebook",
            "Panedwindow", "PanedWindow", "Progressbar", "Radiobutton",
-           "Scrollbar", "Separator", "Sizegrip", "Style", "Treeview"]
+           "Scrollbar", "Separator", "Sizegrip", "Style", "Treeview",
+           # Extensions
+           "OptionMenu"]
 
 import tkinter
 
@@ -1314,3 +1316,46 @@
     def yview(self, *args):
         """Query or modify vertical position of the treeview."""
         return self.tk.call(self._w, "yview", *args)
+
+
+# Extensions
+
+class OptionMenu(Menubutton):
+    """Themed OptionMenu which allows the user to select a value from a
+    menu."""
+
+    def __init__(self, master, variable, value, *values, **kwargs):
+        """Construct a themed OptionMenu widget with the parent master,
+        the resource textvariable set to variable, the initially selected
+        value specified by the value parameter, the other menu values
+        given by *values and an additional keyword argument command."""
+        kw = {'textvariable': variable, 'style': kwargs.pop('style', None)}
+        Menubutton.__init__(self, master, **kw)
+
+        menu = self.__menu = tkinter.Menu(self, name="menu", tearoff=0)
+        self.menuname = menu._w
+
+        callback = kwargs.pop('command', None)
+        if kwargs:
+            raise tkinter.TclError('unknown option -%s' % (
+                next(iter(kwargs.keys()))))
+
+        menu.add_command(label=value,
+                         command=tkinter._setit(variable, value, callback))
+        for v in values:
+            menu.add_command(label=v,
+                             command=tkinter._setit(variable, v, callback))
+        self['menu'] = menu
+
+
+    def __getitem__(self, item):
+        if item == 'menu':
+            return self.__menu
+
+        return Menubutton.__getitem__(self, item)
+
+
+    def destroy(self):
+        """Destroy this widget and the associated menu."""
+        Menubutton.destroy(self)
+        self.__menu = None


More information about the Python-checkins mailing list