[Python-checkins] r64540 - sandbox/trunk/ttk-gsoc/samples/theming.py

guilherme.polo python-checkins at python.org
Thu Jun 26 22:57:36 CEST 2008


Author: guilherme.polo
Date: Thu Jun 26 22:57:35 2008
New Revision: 64540

Log:
Added some widget factories;


Modified:
   sandbox/trunk/ttk-gsoc/samples/theming.py

Modified: sandbox/trunk/ttk-gsoc/samples/theming.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/samples/theming.py	(original)
+++ sandbox/trunk/ttk-gsoc/samples/theming.py	Thu Jun 26 22:57:35 2008
@@ -18,11 +18,14 @@
 
 def available_widgets():
     """Returns a list of Ttk widgets."""
-    widgets = []
+    widgets = {}
     # will discard Style and extension classes
     unwanted_names = ('Style', 'LabeledScale', 'OptionMenu')
     # some widgets contain Vertical and Horizontal layouts
     special = ('Progressbar', 'Scale', 'Scrollbar')
+    sample_text = ('Button', 'Checkbutton', 'Label', 'Radiobutton')
+    sample_text_size = ('Labelframe', )
+    sample_progress = ('Progressbar', )
 
     for name in ttk.__all__:
         if name in unwanted_names:
@@ -33,14 +36,41 @@
             # do not add aliases
             continue
 
+        widget_d = {'factory': None, 'layouts': None}
+
         if name in special:
-            widgets.append((name, ('Horizontal', 'Vertical')))
-        else:
-            widgets.append(name)
+            widget_d['layouts'] = ('Horizontal', 'Vertical')
+
+        if name in sample_text:
+            widget_d['factory'] = widget_text
+        elif name in sample_text_size:
+            widget_d['factory'] = widget_text_size
+        elif name in sample_progress:
+            widget_d['factory'] = widget_progress
+
+        widgets[name] = widget_d
 
     return widgets
 
 
+def widget_text(widget, master, text="Sample", **kw):
+    """Instantiate widget and set its text option to a custom value."""
+    return widget(master, text=text, **kw)
+
+
+def widget_text_size(widget, master, text="Sample", width=150, **kw):
+    """Instantiate widget and set its text option to a custom value and
+    set a size for it."""
+    return widget(master, text=text, width=width, height=width, **kw)
+
+
+def widget_progress(widget, master, maximum=10, **kw):
+    """Instantiate a progressbar and step it a bit."""
+    w = widget(master, maximum=maximum, **kw)
+    w.step(4)
+    return w
+
+
 class AutoScroll(object):
     """Configure the scrollbars for a widget."""
 
@@ -142,14 +172,18 @@
             opts = {'orient': complement.lower()}
 
         # create a sample widget
-        sample = getattr(ttk, widget_name)(self._preview_area, **opts)
+        if widget.get('factory', None):
+            widget_class = getattr(ttk, widget_name)
+            sample = widget['factory'](widget_class, self._preview_area, **opts)
+        else:
+            sample = getattr(ttk, widget_name)(self._preview_area, **opts)
         if widget['class'] is None:
             widget['class'] = "%s%s%s" % (complement,
                 '.' if complement else '', sample.winfo_class())
         sample['style'] = 'Custom.%s' % widget['class']
         if self._current_widget['widget'] is not None:
             self._current_widget['widget'].pack_forget()
-        sample.pack()
+        sample.pack(expand=True)
 
         self._current_widget['layout'] = sample['style']
         self._current_widget['widget'] = sample
@@ -272,7 +306,8 @@
 
         # preview area
         self._preview_area = ttk.Frame(topright, width=200, padding=12)
-        self._preview_area.pack(anchor='center', side='left', expand=True)
+        self._preview_area.pack(anchor='center', side='left', expand=True,
+            fill='both')
 
         # options, images and themes
         frames = ttk.Frame(topright)
@@ -331,19 +366,18 @@
         """Insert available widgets to the treeview."""
         self._widget = {}
         widgets = available_widgets()
-        for widget in widgets:
-            if isinstance(widget, tuple): # horizontal/vertical layout
-                widget, children = widget
-            else:
-                children = ()
+        for name, opts in sorted(widgets.items()):
+            children = opts.pop('layouts') or ()
 
-            parent = self._tv_widgets.insert('', 'end', text=widget)
-            self._widget[widget] = {'tv_item': parent, 'class': None}
+            parent = self._tv_widgets.insert('', 'end', text=name)
+            self._widget[name] = {'tv_item': parent, 'class': None}
+            self._widget[name].update(opts)
 
             for child in children:
-                child_name = '%s.%s' % (child, widget)
+                child_name = '%s.%s' % (child, name)
                 item = self._tv_widgets.insert(parent, 'end', text=child_name)
                 self._widget[child_name] = {'tv_item': item, 'class': None}
+                self._widget[child_name].update(opts)
 
 
 def main(args=None):


More information about the Python-checkins mailing list