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

guilherme.polo python-checkins at python.org
Mon Aug 25 18:12:45 CEST 2008


Author: guilherme.polo
Date: Mon Aug 25 18:12:45 2008
New Revision: 66031

Log:
Added some tests for OptionMenu;
Assorted OptionMenu fixes.


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

Modified: sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/test/test_extensions.py	Mon Aug 25 18:12:45 2008
@@ -8,11 +8,11 @@
 class LabeledScaleTest(unittest.TestCase):
     # XXX
 
-    def setUp(self):
-        self.lscale = ttk.LabeledScale()
+#    def setUp(self):
+#        self.lscale = ttk.LabeledScale()
 
-    def tearDown(self):
-        self.lscale.destroy()
+#    def tearDown(self):
+#        self.lscale.destroy()
 
 
     def test_widget_destroy(self):
@@ -27,7 +27,6 @@
         name = myvar._name
         x = ttk.LabeledScale(variable=myvar)
         x.destroy()
-        root = Tkinter.Tk()
         self.failUnlessEqual(x.tk.globalgetvar(name), myvar.get())
         del myvar
         self.failUnlessRaises(Tkinter.TclError, x.tk.globalgetvar, name)
@@ -64,13 +63,13 @@
         self.failUnlessRaises(Tkinter.TclError, ttk.LabeledScale, a='b')
 
 
-    def test_ranges(self): # XXX finish this
-        self.lscale.pack()
-        self.lscale.wait_visibility()
-        self.lscale.update()
+#    def test_ranges(self): # XXX finish this
+#        self.lscale.pack()
+#        self.lscale.wait_visibility()
+#        self.lscale.update()
 
-        # test decreasing range
-        self.lscale.scale.configure(from_=1, to=-1)
+#        # test decreasing range
+#        self.lscale.scale.configure(from_=1, to=-1)
 #        self.failUnlessEqual(self.lscale.label['text'], 1)
 
 
@@ -78,16 +77,72 @@
     # XXX
 
     def setUp(self):
-        root = Tkinter.Tk()
-        textvar = Tkinter.StringVar()
-        self.optmenu = ttk.OptionMenu(root, textvar)
+        self.root = Tkinter.Tk()
+        self.textvar = Tkinter.StringVar()
 
     def tearDown(self):
-        self.optmenu.destroy()
+        del self.textvar
+        del self.root
+
+
+    def test_initialization(self):
+        self.failUnlessRaises(Tkinter.TclError,
+            ttk.OptionMenu, self.root, self.textvar, invalid='thing')
+
+        optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
+        self.failUnlessEqual(optmenu._variable.get(), 'b')
+
+        self.failUnless(optmenu['menu'])
+        self.failUnless(optmenu['textvariable'])
+
 
+    def test_menu(self):
+        items = ('a', 'b', 'c')
+        default = 'a'
+        optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
+        found_default = False
+        for i in range(len(items)):
+            value = optmenu['menu'].entrycget(i, 'value')
+            self.failUnlessEqual(value, items[i])
+            if value == default:
+                found_default = True
+        self.failUnless(found_default)
+
+        # default shouldn't be in menu if it is not part of values
+        default = 'd'
+        optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
+        curr = None
+        i = 0
+        while True:
+            last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
+            if last == curr:
+                # no more menu entries
+                break
+            self.failIf(curr == default)
+            i += 1
+        self.failUnlessEqual(i, len(items))
+
+        # check that variable is updated correctly
+        optmenu.pack()
+        optmenu.wait_visibility()
+        optmenu['menu'].invoke(0)
+        self.failUnlessEqual(optmenu._variable.get(), items[0])
+
+        # changing to an invalid index shouldn't change the variable
+        self.failUnlessRaises(Tkinter.TclError, optmenu['menu'].invoke, -1)
+        self.failUnlessEqual(optmenu._variable.get(), items[0])
+
+        # specifying a callback
+        success = []
+        def cb_test(item):
+            self.failUnlessEqual(item, items[1])
+            success.append(True)
+        optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
+            *items)
+        optmenu['menu'].invoke(1)
+        if not success:
+            self.fail("Menu callback not invoked")
 
-    def test_parameter_passing(self):
-        pass
 
 
 def test_main():

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 Aug 25 18:12:45 2008
@@ -1509,32 +1509,39 @@
 
 
 class OptionMenu(Menubutton):
-    """Themed OptionMenu which allows the user to select a value from a
-    menu."""
+    """Themed OptionMenu, based after Tkinter's OptionMenu, which allows
+    the user to select a value from a menu."""
 
     def __init__(self, master, variable, default=None, *values, **kwargs):
-        """Construct a themed OptionMenu widget with the parent master,
+        """Construct a themed OptionMenu widget with master as the parent,
         the resource textvariable set to variable, the initially selected
-        value specified by the default parameter, the other menu values
-        given by *values and an additional keyword argument command."""
+        value specified by the default parameter, the menu values given by
+        *values and additional keywords:
+
+            style: stylename
+                Menubutton style.
+            direction: 'above', 'below', 'left', 'right', or 'flush'
+                Menubutton direction.
+            command: callback
+                A callback that will be invoked after selecting an item.
+        """
         kw = {'textvariable': variable, 'style': kwargs.pop('style', None),
               'direction': kwargs.pop('direction', None)}
         Menubutton.__init__(self, master, **kw)
+        self['menu'] = Tkinter.Menu(self, tearoff=False)
 
-        self._menu = Tkinter.Menu(self, name="menu", tearoff=0)
-        self._callback = kwargs.pop('command', None)
         self._variable = variable
+        self._callback = kwargs.pop('command', None)
         if kwargs:
             raise Tkinter.TclError('unknown option -%s' % (
                 kwargs.iterkeys().next()))
 
-        self['menu'] = self._menu
-        self.set_menu(default, *((default, ) + values))
+        self.set_menu(default, *values)
 
 
     def __getitem__(self, item):
         if item == 'menu':
-            return self._menu
+            return self.nametowidget(Menubutton.__getitem__(self, item))
 
         return Menubutton.__getitem__(self, item)
 
@@ -1553,6 +1560,5 @@
 
 
     def destroy(self):
-        """Destroy this widget and the associated menu."""
+        """Destroy this widget."""
         Menubutton.destroy(self)
-        self._menu = None

Modified: sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py	(original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/test/test_extensions.py	Mon Aug 25 18:12:45 2008
@@ -8,11 +8,11 @@
 class LabeledScaleTest(unittest.TestCase):
     # XXX
 
-    def setUp(self):
-        self.lscale = ttk.LabeledScale()
+#    def setUp(self):
+#        self.lscale = ttk.LabeledScale()
 
-    def tearDown(self):
-        self.lscale.destroy()
+#    def tearDown(self):
+#        self.lscale.destroy()
 
 
     def test_widget_destroy(self):
@@ -27,7 +27,6 @@
         name = myvar._name
         x = ttk.LabeledScale(variable=myvar)
         x.destroy()
-        root = tkinter.Tk()
         self.failUnlessEqual(x.tk.globalgetvar(name), myvar.get())
         del myvar
         self.failUnlessRaises(tkinter.TclError, x.tk.globalgetvar, name)
@@ -64,13 +63,13 @@
         self.failUnlessRaises(tkinter.TclError, ttk.LabeledScale, a='b')
 
 
-    def test_ranges(self): # XXX finish this
-        self.lscale.pack()
-        self.lscale.wait_visibility()
-        self.lscale.update()
+#    def test_ranges(self): # XXX finish this
+#        self.lscale.pack()
+#        self.lscale.wait_visibility()
+#        self.lscale.update()
 
-        # test decreasing range
-        self.lscale.scale.configure(from_=1, to=-1)
+#        # test decreasing range
+#        self.lscale.scale.configure(from_=1, to=-1)
 #        self.failUnlessEqual(self.lscale.label['text'], 1)
 
 
@@ -78,16 +77,72 @@
     # XXX
 
     def setUp(self):
-        root = tkinter.Tk()
-        textvar = tkinter.StringVar()
-        self.optmenu = ttk.OptionMenu(root, textvar)
+        self.root = tkinter.Tk()
+        self.textvar = tkinter.StringVar()
 
     def tearDown(self):
-        self.optmenu.destroy()
+        del self.textvar
+        del self.root
+
+
+    def test_initialization(self):
+        self.failUnlessRaises(tkinter.TclError,
+            ttk.OptionMenu, self.root, self.textvar, invalid='thing')
+
+        optmenu = ttk.OptionMenu(self.root, self.textvar, 'b', 'a', 'b')
+        self.failUnlessEqual(optmenu._variable.get(), 'b')
+
+        self.failUnless(optmenu['menu'])
+        self.failUnless(optmenu['textvariable'])
+
 
+    def test_menu(self):
+        items = ('a', 'b', 'c')
+        default = 'a'
+        optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
+        found_default = False
+        for i in range(len(items)):
+            value = optmenu['menu'].entrycget(i, 'value')
+            self.failUnlessEqual(value, items[i])
+            if value == default:
+                found_default = True
+        self.failUnless(found_default)
+
+        # default shouldn't be in menu if it is not part of values
+        default = 'd'
+        optmenu = ttk.OptionMenu(self.root, self.textvar, default, *items)
+        curr = None
+        i = 0
+        while True:
+            last, curr = curr, optmenu['menu'].entryconfigure(i, 'value')
+            if last == curr:
+                # no more menu entries
+                break
+            self.failIf(curr == default)
+            i += 1
+        self.failUnlessEqual(i, len(items))
+
+        # check that variable is updated correctly
+        optmenu.pack()
+        optmenu.wait_visibility()
+        optmenu['menu'].invoke(0)
+        self.failUnlessEqual(optmenu._variable.get(), items[0])
+
+        # changing to an invalid index shouldn't change the variable
+        self.failUnlessRaises(tkinter.TclError, optmenu['menu'].invoke, -1)
+        self.failUnlessEqual(optmenu._variable.get(), items[0])
+
+        # specifying a callback
+        success = []
+        def cb_test(item):
+            self.failUnlessEqual(item, items[1])
+            success.append(True)
+        optmenu = ttk.OptionMenu(self.root, self.textvar, 'a', command=cb_test,
+            *items)
+        optmenu['menu'].invoke(1)
+        if not success:
+            self.fail("Menu callback not invoked")
 
-    def test_parameter_passing(self):
-        pass
 
 
 def test_main():

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 Aug 25 18:12:45 2008
@@ -1509,32 +1509,39 @@
 
 
 class OptionMenu(Menubutton):
-    """Themed OptionMenu which allows the user to select a value from a
-    menu."""
+    """Themed OptionMenu, based after Tkinter's OptionMenu, which allows
+    the user to select a value from a menu."""
 
     def __init__(self, master, variable, default=None, *values, **kwargs):
-        """Construct a themed OptionMenu widget with the parent master,
+        """Construct a themed OptionMenu widget with master as the parent,
         the resource textvariable set to variable, the initially selected
-        value specified by the default parameter, the other menu values
-        given by *values and an additional keyword argument command."""
+        value specified by the default parameter, the menu values given by
+        *values and additional keywords:
+
+            style: stylename
+                Menubutton style.
+            direction: 'above', 'below', 'left', 'right', or 'flush'
+                Menubutton direction.
+            command: callback
+                A callback that will be invoked after selecting an item.
+        """
         kw = {'textvariable': variable, 'style': kwargs.pop('style', None),
               'direction': kwargs.pop('direction', None)}
         Menubutton.__init__(self, master, **kw)
+        self['menu'] = tkinter.Menu(self, tearoff=False)
 
-        self._menu = tkinter.Menu(self, name="menu", tearoff=0)
-        self._callback = kwargs.pop('command', None)
         self._variable = variable
+        self._callback = kwargs.pop('command', None)
         if kwargs:
             raise tkinter.TclError('unknown option -%s' % (
                 next(iter(kwargs.keys()))))
 
-        self['menu'] = self._menu
-        self.set_menu(default, *((default, ) + values))
+        self.set_menu(default, *values)
 
 
     def __getitem__(self, item):
         if item == 'menu':
-            return self._menu
+            return self.nametowidget(Menubutton.__getitem__(self, item))
 
         return Menubutton.__getitem__(self, item)
 
@@ -1553,6 +1560,5 @@
 
 
     def destroy(self):
-        """Destroy this widget and the associated menu."""
+        """Destroy this widget."""
         Menubutton.destroy(self)
-        self._menu = None


More information about the Python-checkins mailing list