[Python-checkins] r62664 - sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py

guilherme.polo python-checkins at python.org
Sat May 3 19:04:33 CEST 2008


Author: guilherme.polo
Date: Sat May  3 19:04:32 2008
New Revision: 62664

Log:
theme_create for Ttk styling has been wrapped;

_format_mapdict correctly accepts the state '' now;

moved most of theme_settings code to a separate function 
(_script_from_settings) so theme_create can use it too.


Modified:
   sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py

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	Sat May  3 19:04:32 2008
@@ -74,12 +74,37 @@
                 # lets believe it is a sequence and group these multiple states
                 state = "{%s}" % ' '.join(state) 
 
+            if not state: 
+                # empty string in Python is equivalent to {} in Tcl, and it may
+                # be used to indicate the "normal" state
+                state = "{}"
+
             opt_val.append("%s %s" % (state, val))
 
         opts.append(("-%s" % opt, format % ' '.join(opt_val)))
     
     return Tkinter._flatten(opts)
 
+def _script_from_settings(settings):
+    """Returns an appropriate script, based on settings, according to 
+    theme_settings definition to be used by theme_settings and 
+    theme_create."""
+    script = []
+    # a script will be generated according to settings passed, which
+    # will then be evaluated by Tcl
+    for name, opts in settings.iteritems():
+        # format 'configure' according to Tcl code
+        if opts.get('configure'):
+            s = ' '.join(map(str, _format_optdict(opts['configure'])))
+            script.append("ttk::style configure %s %s;" % (name, s))
+
+        # format 'map' according to Tcl code
+        if opts.get('map'):
+            s = ' '.join(map(str, _format_mapdict(opts['map'], True)))
+            script.append("ttk::style map %s %s;" % (name, s))
+
+    return '\n'.join(script)
+
 def _dict_from_tcltuple(t):
     """Break tuple in pairs then build the return dict.
 
@@ -201,8 +226,21 @@
         return self.tk.call(self._name, "element", "options", element)
 
 
-    def theme_create(self, themeName, **kw):
-        raise NotImplementedError
+    def theme_create(self, themeName, parent=None, settings=None):
+        """Creates a new theme.
+
+        It is an error if themeName already exists. If parent is 
+        specified, the new theme will inherit styles, elements and 
+        layouts from the specified parent theme. If options are present,
+        they are expected to have the same syntax used for theme_settings."""
+        script = _script_from_settings(settings) if settings else ''
+    
+        if parent:
+            self.tk.call(self._name, "theme", "create", themeName,
+                "-parent", parent, "-settings", script)
+        else:
+            self.tk.call(self._name, "theme", "create", themeName, 
+                "-settings", script)
 
 
     def theme_settings(self, themeName, settings):
@@ -213,21 +251,7 @@
         keys 'configure' and 'map'. 'configure' and 'map' are expected to 
         have the same format as specified by the methods configure and map 
         respectively."""
-        script = []
-        # a script will be generated according to settings passed, which
-        # will then be evaluated by Tcl
-        for name, opts in settings.iteritems():
-            # format 'configure' according to Tcl code
-            if opts.get('configure'):
-                s = ' '.join(map(str, _format_optdict(opts['configure'])))
-                script.append("%s configure %s %s;" % (self._name, name, s))
-
-            # format 'map' according to Tcl code
-            if opts.get('map'):
-                s = ' '.join(map(str, _format_mapdict(opts['map'], True)))
-                script.append("%s map %s %s;" % (self._name, name, s))
-
-        script = '\n'.join(script)
+        script = _script_from_settings(settings)
         self.tk.call(self._name, "theme", "settings", themeName, script)
 
 


More information about the Python-checkins mailing list