[Python-checkins] r63566 - in sandbox/trunk/ttk-gsoc: Doc/library/ttk.rst samples/plastik_theme.py src/2.x/ttk.py src/3.x/ttk.py

guilherme.polo python-checkins at python.org
Fri May 23 19:28:50 CEST 2008


Author: guilherme.polo
Date: Fri May 23 19:28:50 2008
New Revision: 63566

Log:
Made the format accepted by _format_mapdict simpler

Modified:
   sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
   sandbox/trunk/ttk-gsoc/samples/plastik_theme.py
   sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
   sandbox/trunk/ttk-gsoc/src/3.x/ttk.py

Modified: sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
==============================================================================
--- sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst	(original)
+++ sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst	Fri May 23 19:28:50 2008
@@ -1120,8 +1120,9 @@
       Query or sets dynamic values of the specified option(s) in *style*.
 
       Each key in kw is an option and each value should be a list or a
-      tuple (usually) containing state(s)/value pairs. state(s) is/are
-      expected to be a list or a tuple (again).
+      tuple (usually) containing statespecs grouped in tuples, or list, or
+      something else of your preference. A statespec is compound of one or more
+      states and then a value.
 
       An example may make it more understandable::
 
@@ -1132,10 +1133,8 @@
 
          style = ttk.Style()
          style.map("C.TButton",
-             foreground=[(['pressed'], 'red'),
-                         (['active'], 'blue')],
-             background=[(['pressed', '!disabled'], 'black'),
-                         (['active'], 'white')]
+             foreground=[('pressed', 'red'), ('active', 'blue')],
+             background=[('pressed', '!disabled', 'black'), ('active', 'white')]
              )
 
          colored_btn = ttk.Button(text="Test", style="C.TButton").pack()
@@ -1145,10 +1144,10 @@
 
       There is a thing to note in this previous short example:
 
-       * The order of the (state, value) sequences for an option does matter,
-         if you changed the order to [(('active', ), 'blue'), (('pressed', ),
-         'red')] in the foreground option, for example, you would get a blue
-         foreground when the widget were in active or pressed states.
+       * The order of the (states, value) sequences for an option does matter,
+         if you changed the order to [('active', 'blue'), ('pressed', 'red')] 
+         in the foreground option, for example, you would get a blue foreground
+         when the widget were in active or pressed states.
 
 
    .. method:: lookup(style, option[, state=None[, default=None]])
@@ -1281,11 +1280,11 @@
             "TCombobox": {
                 "configure": {"padding": 5},
                 "map": {
-                    "background": [(("active", ), "green2"),
-                                   (("!disabled", ), "green4")],
-                    "fieldbackground": [(("!disabled", ), "green3")],
-                    "foreground": [(("focus", ), "OliveDrab1"),
-                                   (("!disabled", ), "OliveDrab2")]
+                    "background": [("active", "green2"),
+                                   ("!disabled", "green4")],
+                    "fieldbackground": [("!disabled", "green3")],
+                    "foreground": [("focus", "OliveDrab1"),
+                                   ("!disabled", "OliveDrab2")]
                 }
             }
          })
@@ -1321,11 +1320,14 @@
  * sticky: nswe
     Specifies where the element is placed inside its allocated parcel.
 
- * expand: 1 or 0 XXX
+ * expand: 0 or 1
+    XXX
 
- * border: XXX
+ * border: 0 or 1
+    XXX
 
- * unit: XXX
+ * unit: 0 or 1
+    XXX
 
  * children: [sublayout... ]
     Specifies a list of elements to place inside the element. Each

Modified: sandbox/trunk/ttk-gsoc/samples/plastik_theme.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/samples/plastik_theme.py	(original)
+++ sandbox/trunk/ttk-gsoc/samples/plastik_theme.py	Fri May 23 19:28:50 2008
@@ -60,7 +60,7 @@
                  "fieldbackground": colors['frame'],
                  "font": "TkDefaultFont",
                  "borderwidth": 1},
-            "map": {"foreground": [(("disabled", ), colors['disabledfg'])]}
+            "map": {"foreground": [("disabled", colors['disabledfg'])]}
         },
 
         "Vertical.TScrollbar": {"layout": [
@@ -127,41 +127,41 @@
         "TNotebook": {"configure": {"tabmargins": [0, 2, 0, 0]}},
         "TNotebook.tab": {
             "configure": {"padding": [6, 2, 6, 2], "expand": [0, 0, 2]},
-            "map": {"expand": [(("selected", ), [1, 2, 4, 2])]}
+            "map": {"expand": [("selected", [1, 2, 4, 2])]}
         },
         "Treeview": {"configure": {"padding": 0}},
 
         # elements
         "Button.button": {"element create":
             ("image", 'button-n',
-                (("pressed", ), 'button-p'), (("active", ), 'button-h'),
+                ("pressed", 'button-p'), ("active", 'button-h'),
                 {"border": [4, 10], "padding": 4, "sticky":"ewns"}
             )
         },
 
         "Toolbutton.button": {"element create":
             ("image", 'tbutton-n',
-                (("selected", ), 'tbutton-p'), (("pressed", ), 'tbutton-p'),
-                (("active", ), 'tbutton-h'),
+                ("selected", 'tbutton-p'), ("pressed", 'tbutton-p'),
+                ("active", 'tbutton-h'),
                 {"border": [4, 9], "padding": 3, "sticky": "news"}
             )
         },
 
         "Checkbutton.indicator": {"element create":
             ("image", 'check-nu',
-                (("active", "selected"), 'check-hc'),
-                (("pressed", "selected"), 'check-pc'),
-                (("active", ), 'check-hu'),
-                (("selected", ), 'check-nc'),
+                ('active', 'selected', 'check-hc'),
+                ('pressed', 'selected', 'check-pc'),
+                ('active', 'check-hu'),
+                ("selected", 'check-nc'),
                 {"sticky": ''}
             )
         },
 
         "Radiobutton.indicator": {"element create":
             ("image", 'radio-nu',
-                (("active", "selected"), 'radio-hc'),
-                (("pressed", "selected"), 'radio-pc'),
-                (("active", ), 'radio-hu'), (("selected", ), 'radio-nc'),
+                ('active', 'selected', 'radio-hc'),
+                ('pressed', 'selected', 'radio-pc'),
+                ('active', 'radio-hu'), ('selected', 'radio-nc'),
                 {"sticky": ''}
             )
         },
@@ -178,18 +178,18 @@
         "Vertical.Scrollbar.grip": {"element create": ("image", 'vsb-g')},
         "Vertical.Scrollbar.trough": {"element create": ("image", 'vsb-t')},
         "Scrollbar.uparrow": {"element create":
-            ("image", 'arrowup-n', (("pressed", ), 'arrowup-p'), {"sticky": ''})
+            ("image", 'arrowup-n', ("pressed", 'arrowup-p'), {"sticky": ''})
         },
         "Scrollbar.downarrow": {"element create":
             ("image", 'arrowdown-n',
-            (("pressed", ), 'arrowdown-p'), {'sticky': ''})
+            ("pressed", 'arrowdown-p'), {'sticky': ''})
         },
         "Scrollbar.leftarrow": {"element create":
             ("image", 'arrowleft-n',
-            (("pressed", ), 'arrowleft-p'), {'sticky': ''})
+            ("pressed", 'arrowleft-p'), {'sticky': ''})
         },
         "Scrollbar.rightarrow": {"element create":
-            ("image", 'arrowright-n', (("pressed", ), 'arrowright-p'),
+            ("image", 'arrowright-n', ("pressed", 'arrowright-p'),
             {'sticky': ''})
         },
 
@@ -208,7 +208,7 @@
 
         "Entry.field": {"element create":
             ("image", 'entry-n',
-                (("focus", ), 'entry-f'),
+                ("focus", 'entry-f'),
                 {'border': 2, 'padding': [3, 4], 'sticky': 'news'}
             )
         },
@@ -219,7 +219,7 @@
 
         "Menubutton.button": {"element create":
             ("image", 'combo-r',
-                (('active', ), 'combo-ra'),
+                ('active', 'combo-ra'),
                 {'sticky': 'news', 'border': [4, 6, 24, 15],
                  'padding': [4, 4, 5]}
             )
@@ -230,10 +230,10 @@
 
         "Combobox.field": {"element create":
             ("image", 'combo-n',
-                (('readonly', 'active'), 'combo-ra'),
-                (('focus', 'active'), 'combo-fa'),
-                (('active', ), 'combo-a'), (('!readonly', 'focus'), 'combo-f'),
-                (('readonly', ), 'combo-r'),
+                ('readonly', 'active', 'combo-ra'),
+                ('focus', 'active', 'combo-fa'),
+                ('active', 'combo-a'), ('!readonly', 'focus', 'combo-f'),
+                ('readonly', 'combo-r'),
                 {'border': [4, 6, 24, 15], 'padding': [4, 4, 5],
                  'sticky': 'news'}
             )
@@ -247,7 +247,7 @@
         },
         "Notebook.tab": {"element create":
             ("image", 'notebook-tn',
-                (("selected", ), 'notebook-ts'), (("active", ), 'notebook-ta'),
+                ("selected", 'notebook-ts'), ("active", 'notebook-ta'),
                 {'padding': [0, 2, 0, 0], 'border': [4, 10, 4, 10]}
             )
         },
@@ -264,7 +264,7 @@
 
         "Treeheading.cell": {"element create":
             ("image", 'tree-n',
-                (("pressed", ), 'tree-p'),
+                ("pressed", 'tree-p'),
                 {'border': [4, 10], 'padding': 4, 'sticky': 'news'}
             )
         }

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	Fri May 23 19:28:50 2008
@@ -23,6 +23,8 @@
 
 import Tkinter
 
+_flatten = Tkinter._flatten
+
 # Verify if Tk is new enough to not need Tile checking
 _REQUIRE_TILE = True if Tkinter.TkVersion < 8.5 else False
 
@@ -71,14 +73,17 @@
 
         opts.append(("-%s" % opt, value))
 
-    return Tkinter._flatten(opts)
+    return _flatten(opts)
 
 def _format_mapdict(mapdict, script=False):
     """Formats mapdict to pass it to tk.call.
     
     E.g. (script=False):
-      {'background': [(['active'], 'white'), (['focus'], 'blue')]} returns:
-      ('-background', 'active white focus blue')"""
+      {'expand': [('active', 'selected', 'grey'), ('focus', [1, 2, 3, 4])]}
+
+      returns:
+
+      ('-expand', '{active selected} grey focus {1, 2, 3, 4}')"""
     # if caller passes a Tcl script to tk.call, all the values need to
     # be grouped into words (arguments to a command in Tcl dialect)
     format = "%s" if not script else "{%s}"
@@ -89,7 +94,8 @@
         opt_val = []
         # each value in mapdict is expected to be a sequence, where each item
         # is another sequence containing a state (or several) and a value for it
-        for state, val in value:
+        for statespec in value:
+            state, val = statespec[:-1], statespec[-1]
             if not state:
                 # empty state denotes the "normal" state, format it to Tcl
                 state = "{}"
@@ -106,7 +112,7 @@
 
         opts.append(("-%s" % opt, format % ' '.join(opt_val)))
 
-    return Tkinter._flatten(opts)
+    return _flatten(opts)
 
 def _format_elemcreate(etype, script=False, *args, **kw):
     """Formats args and kw according to the given element factory etype."""
@@ -253,7 +259,8 @@
 
         elif val and hasattr(val, "__len__") and hasattr(val[0], "typename"):
             split_it = lambda ob: getattr(ob, 'typename', None) == 'StateSpec'
-            val = [str(v).split() if split_it(v) else v for v in val]
+            v = [str(v).split() if split_it(v) else v for v in val]
+            val = [_flatten(v[i] + [v[i + 1]]) for i in range(0, len(v), 2)]
 
         opts.append((str(opt)[opt_start:], val))
 
@@ -332,8 +339,9 @@
         style.
 
         Each key in kw is an option and each value should be a list or a
-        tuple (usually) containing state(s)/value pairs. state(s) is/are
-        expected to be a list or a tuple (again)."""
+        tuple (usually) containing statespecs grouped in tuples, or list,
+        or something else of your preference. A statespec is compound of
+        one or more states and then a value."""
         return _dict_from_tcltuple(self.tk.call(self._name, "map", style,
                                                 *(_format_mapdict(kw))))
 

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	Fri May 23 19:28:50 2008
@@ -23,6 +23,8 @@
 
 import tkinter
 
+_flatten = tkinter._flatten
+
 # Verify if Tk is new enough to not need Tile checking
 _REQUIRE_TILE = True if tkinter.TkVersion < 8.5 else False
 
@@ -71,14 +73,17 @@
 
         opts.append(("-%s" % opt, value))
 
-    return tkinter._flatten(opts)
+    return _flatten(opts)
 
 def _format_mapdict(mapdict, script=False):
     """Formats mapdict to pass it to tk.call.
     
     E.g. (script=False):
-      {'background': [(['active'], 'white'), (['focus'], 'blue')]} returns:
-      ('-background', 'active white focus blue')"""
+      {'expand': [('active', 'selected', 'grey'), ('focus', [1, 2, 3, 4])]}
+
+      returns:
+
+      ('-expand', '{active selected} grey focus {1, 2, 3, 4}')"""
     # if caller passes a Tcl script to tk.call, all the values need to
     # be grouped into words (arguments to a command in Tcl dialect)
     format = "%s" if not script else "{%s}"
@@ -89,7 +94,8 @@
         opt_val = []
         # each value in mapdict is expected to be a sequence, where each item
         # is another sequence containing a state (or several) and a value for it
-        for state, val in value:
+        for statespec in value:
+            state, val = statespec[:-1], statespec[-1]
             if not state:
                 # empty state denotes the "normal" state, format it to Tcl
                 state = "{}"
@@ -106,7 +112,7 @@
 
         opts.append(("-%s" % opt, format % ' '.join(opt_val)))
 
-    return tkinter._flatten(opts)
+    return _flatten(opts)
 
 def _format_elemcreate(etype, script=False, *args, **kw):
     """Formats args and kw according to the given element factory etype."""
@@ -253,7 +259,8 @@
 
         elif val and hasattr(val, "__len__") and hasattr(val[0], "typename"):
             split_it = lambda ob: getattr(ob, 'typename', None) == 'StateSpec'
-            val = [str(v).split() if split_it(v) else v for v in val]
+            v = [str(v).split() if split_it(v) else v for v in val]
+            val = [_flatten(v[i] + [v[i + 1]]) for i in range(0, len(v), 2)]
 
         opts.append((str(opt)[opt_start:], val))
 
@@ -332,8 +339,9 @@
         style.
 
         Each key in kw is an option and each value should be a list or a
-        tuple (usually) containing state(s)/value pairs. state(s) is/are
-        expected to be a list or a tuple (again)."""
+        tuple (usually) containing statespecs grouped in tuples, or list,
+        or something else of your preference. A statespec is compound of
+        one or more states and then a value."""
         return _dict_from_tcltuple(self.tk.call(self._name, "map", style,
                                                 *(_format_mapdict(kw))))
 


More information about the Python-checkins mailing list