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

guilherme.polo python-checkins at python.org
Sat Jun 7 00:14:52 CEST 2008


Author: guilherme.polo
Date: Sat Jun  7 00:14:52 2008
New Revision: 63999

Log:
New support class: LabeledScale


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

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	Sat Jun  7 00:14:52 2008
@@ -21,7 +21,7 @@
            "PanedWindow", "Progressbar", "Radiobutton", "Scale", "Scrollbar",
            "Separator", "Sizegrip", "Style", "Treeview",
            # Extensions
-           "OptionMenu"]
+           "LabeledScale", "OptionMenu"]
 
 import Tkinter
 
@@ -1359,6 +1359,42 @@
 
 # Extensions
 
+class LabeledScale(Frame):
+    """A Ttk Scale widget with a Ttk Label widget over it indicating its
+    current value.
+    
+    The Ttk Scale can be accessed through instance.scale, and Ttk Label
+    can be accessed through instance.label"""
+
+    def __init__(self, master, variable, from_=0, to=10, **kw):
+        """Construct a LabeledScale with parent master, a variable to be
+        associated with the Ttk Scale widget and its range."""
+        Frame.__init__(self, master, **kw)
+        self.label = Label(self)
+        self.scale = Scale(self, variable=variable, from_=from_, to=to)
+
+        tmp = Label(self).pack() # place holder
+        self.label.place(anchor='n')
+        self.scale.pack(side='bottom', fill='x')
+
+        self._variable = variable
+        self._variable.trace_variable('w', self._adjust)
+
+        self.scale.bind('<Configure>', self._adjust)
+        self.scale.bind('<Map>', self._adjust)
+
+
+    def _adjust(self, *args):
+        """Adjust the label position according to the scale."""
+        self.update()
+        self.label['text'] = self._variable.get()
+        x, y = self.scale.coords()
+
+        y = self.scale.winfo_y() - self.label.winfo_reqheight()
+        x = x + self.scale.winfo_x()
+        self.label.place_configure(x=x, y=y)
+
+
 class OptionMenu(Menubutton):
     """Themed OptionMenu which allows the user to select a value from a
     menu."""

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	Sat Jun  7 00:14:52 2008
@@ -21,7 +21,7 @@
            "PanedWindow", "Progressbar", "Radiobutton", "Scale", "Scrollbar",
            "Separator", "Sizegrip", "Style", "Treeview",
            # Extensions
-           "OptionMenu"]
+           "LabeledScale", "OptionMenu"]
 
 import tkinter
 
@@ -1359,6 +1359,42 @@
 
 # Extensions
 
+class LabeledScale(Frame):
+    """A Ttk Scale widget with a Ttk Label widget over it indicating its
+    current value.
+    
+    The Ttk Scale can be accessed through instance.scale, and Ttk Label
+    can be accessed through instance.label"""
+
+    def __init__(self, master, variable, from_=0, to=10, **kw):
+        """Construct a LabeledScale with parent master, a variable to be
+        associated with the Ttk Scale widget and its range."""
+        Frame.__init__(self, master, **kw)
+        self.label = Label(self)
+        self.scale = Scale(self, variable=variable, from_=from_, to=to)
+
+        tmp = Label(self).pack() # place holder
+        self.label.place(anchor='n')
+        self.scale.pack(side='bottom', fill='x')
+
+        self._variable = variable
+        self._variable.trace_variable('w', self._adjust)
+
+        self.scale.bind('<Configure>', self._adjust)
+        self.scale.bind('<Map>', self._adjust)
+
+
+    def _adjust(self, *args):
+        """Adjust the label position according to the scale."""
+        self.update()
+        self.label['text'] = self._variable.get()
+        x, y = self.scale.coords()
+
+        y = self.scale.winfo_y() - self.label.winfo_reqheight()
+        x = x + self.scale.winfo_x()
+        self.label.place_configure(x=x, y=y)
+
+
 class OptionMenu(Menubutton):
     """Themed OptionMenu which allows the user to select a value from a
     menu."""


More information about the Python-checkins mailing list