[Python-checkins] r64013 - in sandbox/trunk/ttk-gsoc: samples/widget_state.py src/2.x/ttk.py src/3.x/ttk.py
guilherme.polo
python-checkins at python.org
Sat Jun 7 16:00:11 CEST 2008
Author: guilherme.polo
Date: Sat Jun 7 16:00:10 2008
New Revision: 64013
Log:
Updated and reorganized the extension class LabeledScale. Now it auto creates a
variable if one isn't passed, and supports label at top or bottom.
Modified:
sandbox/trunk/ttk-gsoc/samples/widget_state.py
sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
Modified: sandbox/trunk/ttk-gsoc/samples/widget_state.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/samples/widget_state.py (original)
+++ sandbox/trunk/ttk-gsoc/samples/widget_state.py Sat Jun 7 16:00:10 2008
@@ -3,7 +3,6 @@
import ttk
-PY3K = True if sys.version_info[0] > 2 else False
states = ['active', 'disabled', 'focus', 'pressed', 'selected',
'background', 'readonly', 'alternate', 'invalid']
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 16:00:10 2008
@@ -1359,50 +1359,84 @@
# Extensions
-class LabeledScale(Frame):
+class LabeledScale(Frame, object):
"""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):
+ def __init__(self, master=None, variable=None, from_=0, to=10, **kw):
"""Construct a LabeledScale with parent master, a variable to be
- associated with the Ttk Scale widget and its range."""
+ associated with the Ttk Scale widget and its range. If variable is
+ not specified, a Tkinter.IntVar is created.
+
+ WIDGET-SPECIFIC OPTIONS
+
+ compound: 'top' or 'bottom'
+ Specifies how to display the label relative to the scale.
+ Defaults to 'top'.
+ """
+ self._label_top = kw.pop('compound', 'top') == 'top'
Frame.__init__(self, master, **kw)
+ self._variable = variable or Tkinter.IntVar(master, value=from_)
+
self.label = Label(self)
- self.scale = Scale(self, variable=variable, from_=from_, to=to)
+ self.scale = Scale(self, variable=self._variable, from_=from_, to=to)
- tmp = Label(self).pack() # place holder
- self.label.place(anchor='n')
- self.scale.pack(side='bottom', fill='x')
+ # position scale and label according to the compound option
+ scale_side = 'bottom' if self._label_top else 'top'
+ label_side = 'top' if scale_side == 'bottom' else 'bottom'
+ self.scale.pack(side=scale_side, fill='x')
+ tmp = Label(self).pack(side=label_side) # place holder
+ self.label.place(anchor='n' if label_side == 'top' else 's')
- self._variable = variable
+ # update the label as scale or variable changes
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."""
+ newval = self._variable.get()
+ if not self.scale['from'] <= newval <= self.scale['to']:
+ return
+
self.update()
- self.label['text'] = self._variable.get()
- x, y = self.scale.coords()
+ self.label['text'] = newval
- y = self.scale.winfo_y() - self.label.winfo_reqheight()
+ x, y = self.scale.coords()
+ if self._label_top:
+ y = self.scale.winfo_y() - self.label.winfo_reqheight()
+ else:
+ y = self.scale.winfo_reqheight() + self.label.winfo_reqheight()
x = x + self.scale.winfo_x()
+
self.label.place_configure(x=x, y=y)
+ def _get_value(self):
+ """Return current scale value."""
+ return self._variable.get()
+
+
+ def _set_value(self, val):
+ """Set new scale value."""
+ self._variable.set(val)
+
+
+ value = property(_get_value, _set_value)
+
+
class OptionMenu(Menubutton):
"""Themed OptionMenu which allows the user to select a value from a
menu."""
- def __init__(self, master, variable, value, *values, **kwargs):
+ def __init__(self, master, variable, default=None, *values, **kwargs):
"""Construct a themed OptionMenu widget with the parent master,
the resource textvariable set to variable, the initially selected
- value specified by the value parameter, the other menu values
+ value specified by the default parameter, the other menu values
given by *values and an additional keyword argument command."""
kw = {'textvariable': variable, 'style': kwargs.pop('style', None),
'direction': kwargs.pop('direction', None)}
@@ -1416,7 +1450,7 @@
kwargs.iterkeys().next()))
self['menu'] = self._menu
- self.set_menu(value, *((value, ) + values))
+ self.set_menu(default, *((default, ) + values))
def __getitem__(self, item):
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 16:00:10 2008
@@ -1366,43 +1366,77 @@
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):
+ def __init__(self, master=None, variable=None, from_=0, to=10, **kw):
"""Construct a LabeledScale with parent master, a variable to be
- associated with the Ttk Scale widget and its range."""
+ associated with the Ttk Scale widget and its range. If variable is
+ not specified, a tkinter.IntVar is created.
+
+ WIDGET-SPECIFIC OPTIONS
+
+ compound: 'top' or 'bottom'
+ Specifies how to display the label relative to the scale.
+ Defaults to 'top'.
+ """
+ self._label_top = kw.pop('compound', 'top') == 'top'
Frame.__init__(self, master, **kw)
+ self._variable = variable or tkinter.IntVar(master, value=from_)
+
self.label = Label(self)
- self.scale = Scale(self, variable=variable, from_=from_, to=to)
+ self.scale = Scale(self, variable=self._variable, from_=from_, to=to)
- tmp = Label(self).pack() # place holder
- self.label.place(anchor='n')
- self.scale.pack(side='bottom', fill='x')
+ # position scale and label according to the compound option
+ scale_side = 'bottom' if self._label_top else 'top'
+ label_side = 'top' if scale_side == 'bottom' else 'bottom'
+ self.scale.pack(side=scale_side, fill='x')
+ tmp = Label(self).pack(side=label_side) # place holder
+ self.label.place(anchor='n' if label_side == 'top' else 's')
- self._variable = variable
+ # update the label as scale or variable changes
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."""
+ newval = self._variable.get()
+ if not self.scale['from'] <= newval <= self.scale['to']:
+ return
+
self.update()
- self.label['text'] = self._variable.get()
- x, y = self.scale.coords()
+ self.label['text'] = newval
- y = self.scale.winfo_y() - self.label.winfo_reqheight()
+ x, y = self.scale.coords()
+ if self._label_top:
+ y = self.scale.winfo_y() - self.label.winfo_reqheight()
+ else:
+ y = self.scale.winfo_reqheight() + self.label.winfo_reqheight()
x = x + self.scale.winfo_x()
+
self.label.place_configure(x=x, y=y)
+ def _get_value(self):
+ """Return current scale value."""
+ return self._variable.get()
+
+
+ def _set_value(self, val):
+ """Set new scale value."""
+ self._variable.set(val)
+
+
+ value = property(_get_value, _set_value)
+
+
class OptionMenu(Menubutton):
"""Themed OptionMenu which allows the user to select a value from a
menu."""
- def __init__(self, master, variable, value, *values, **kwargs):
+ def __init__(self, master, variable, default=None, *values, **kwargs):
"""Construct a themed OptionMenu widget with the parent master,
the resource textvariable set to variable, the initially selected
- value specified by the value parameter, the other menu values
+ value specified by the default parameter, the other menu values
given by *values and an additional keyword argument command."""
kw = {'textvariable': variable, 'style': kwargs.pop('style', None),
'direction': kwargs.pop('direction', None)}
@@ -1416,7 +1450,7 @@
next(iter(kwargs.keys()))))
self['menu'] = self._menu
- self.set_menu(value, *((value, ) + values))
+ self.set_menu(default, *((default, ) + values))
def __getitem__(self, item):
More information about the Python-checkins
mailing list