[Python-checkins] r62601 - in sandbox/trunk/ttk-gsoc: Doc/library/ttk.rst Lib/lib-tk/Ttk.py

guilherme.polo python-checkins at python.org
Wed Apr 30 21:54:13 CEST 2008


Author: guilherme.polo
Date: Wed Apr 30 21:54:13 2008
New Revision: 62601

Log:
Improved Style's map wrapping and documented it


Modified:
   sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
   sandbox/trunk/ttk-gsoc/Lib/lib-tk/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	Wed Apr 30 21:54:13 2008
@@ -183,6 +183,47 @@
 
 .. class:: Style
 
+   .. method:: map(style, **kw)
+
+      Sets dynamic values of the specified option(s) in *style*. 
+      
+      Each key in *kw* is an option and each value should be a sequence. 
+      Each of these sequences have to have a state, or a group of states, 
+      and a value, grouped in yet another sequence. Note that a group of 
+      states is expected to be a sequence, otherwise a single string should be
+      passed as state.
+
+      An example may make it more understandable::
+      
+         import Tkinter
+         import Ttk
+
+         root = Tkinter.Tk()
+
+         style = Ttk.Style()
+         style.map("C.TButton", 
+             foreground=[('pressed', 'red'), 
+                         ('active', 'blue')],
+             background=[(('pressed', '!disabled'), 'black'), 
+                         ('active', 'white')]
+             )
+
+         colored_btn = Ttk.Button(text="Test", style="C.TButton").pack()
+
+         root.mainloop()
+
+
+      There are two things 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')] 
+         for the foreground, for example, you would get a blue foreground when 
+         the widget were in active or pressed states. 
+       * In the background option, (('pressed', '!disabled'), 'black') was 
+         especified for grouping the states 'pressed' and '!disabled', but it
+         has no different effect than using just 'pressed' for this example, 
+         this was done just to explain the documentation.
+
 
 Others new features
 -------------------

Modified: sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py	Wed Apr 30 21:54:13 2008
@@ -63,9 +63,29 @@
         return self.tk.call(self._name, "configure", style, *opts)
 
 
-    def map(self, style, statespec):
-        # XXX Missing docstring
-        self.tk.call(self._name, "map", style, statespec)
+    def map(self, style, **kw):
+        """Sets dynamic values of the specified option(s) in given style."""
+        opts = []
+
+        # format kw to pass it to tk
+        for opt, value in kw.iteritems():
+
+            opt_val = []
+            # each value in kw is expected to be a sequence, where each item
+            # is a another sequence containg a state (or several) and a value 
+            # for it
+            for state, val in value:
+                if not isinstance(state, basestring):
+                    # lets believe it is a sequence and format these multiple 
+                    # states according to Tk documentation
+                    state = "{%s}" % ' '.join(state) 
+
+                opt_val.append("%s %s" % (state, val))
+
+            opts.append(("-%s" % opt, ' '.join(opt_val)))
+
+        opts = Tkinter._flatten(opts)
+        self.tk.call(self._name, "map", style, *opts)
 
 
     def lookup(self, style, option, state=None, default=None):


More information about the Python-checkins mailing list