[Python-checkins] r63592 - in sandbox/trunk/ttk-gsoc: Doc/library/ttk.rst src/2.x/ttk.py src/3.x/ttk.py
guilherme.polo
python-checkins at python.org
Sat May 24 22:03:02 CEST 2008
Author: guilherme.polo
Date: Sat May 24 22:03:01 2008
New Revision: 63592
Log:
Simplified option query for configure and map methods
Modified:
sandbox/trunk/ttk-gsoc/Doc/library/ttk.rst
sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
sandbox/trunk/ttk-gsoc/src/3.x/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 Sat May 24 22:03:01 2008
@@ -1091,7 +1091,7 @@
This class is used to manipulate the style database.
- .. method:: configure(style, **kw)
+ .. method:: configure(style, query_opt=None, **kw)
Query or sets the default value of the specified option(s) in *style*.
@@ -1115,7 +1115,7 @@
root.mainloop()
- .. method:: map(style, **kw)
+ .. method:: map(style, query_opt=None, **kw)
Query or sets dynamic values of the specified option(s) in *style*.
@@ -1145,7 +1145,7 @@
There is a thing to note in this previous short example:
* The order of the (states, value) sequences for an option does matter,
- if you changed the order to [('active', 'blue'), ('pressed', 'red')]
+ if you changed the order to [('active', 'blue'), ('pressed', 'red')]
in the foreground option, for example, you would get a blue foreground
when the widget were in active or pressed states.
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 May 24 22:03:01 2008
@@ -253,19 +253,24 @@
for i in range(len(ttuple) / 2):
opt, val = ttuple[i * 2:i * 2 + 2]
- if hasattr(val, "find") and val.find(' ') != - 1:
- # this could be the padding option
+ if hasattr(val, 'find') and val.find(' ') != -1:
+ # this could be the padding option ("left top right bottom")
val = map(int, val.split())
elif val and hasattr(val, "__len__") and hasattr(val[0], "typename"):
- split_it = lambda ob: getattr(ob, 'typename', None) == 'StateSpec'
- v = [str(v).split() if split_it(v) else v for v in val]
- val = [_flatten(v[i] + [v[i + 1]]) for i in range(0, len(v), 2)]
+ val = _list_from_statespec(val)
opts.append((str(opt)[opt_start:], val))
return dict(opts)
+def _list_from_statespec(stuple):
+ """Construct a list from the given statespec tuple according to the
+ accepted statespec accepted by _format_mapdict."""
+ split_it = lambda ob: getattr(ob, 'typename', None) == 'StateSpec'
+ val = [str(val).split() if split_it(val) else val for val in stuple]
+ return [_flatten(val[i] + [val[i + 1]]) for i in range(0, len(val), 2)]
+
def _list_from_layouttuple(ltuple):
"""Construct a list from the tuple returned by ttk::layout, this is
somewhat the reverse of _format_layoutlist."""
@@ -304,7 +309,7 @@
options = _format_optdict(options)
res = func(*(args + options))
- if len(options) % 2: # option specified without a value
+ if len(options) % 2: # option specified without a value, return its value
return res
return _dict_from_tcltuple(res)
@@ -324,17 +329,21 @@
self.tk = master.tk
- def configure(self, style, **kw):
+ def configure(self, style, query_opt=None, **kw):
"""Query or sets the default value of the specified option(s) in
style.
Each key in kw is an option and each value is either a string or
a sequence identifying the value for that option."""
+ if query_opt:
+ query_opt = '' if query_opt.startswith('-') else '-' + query_opt
+ return self.tk.call(self._name, "configure", style, query_opt)
+
return _dict_from_tcltuple(self.tk.call(self._name, "configure", style,
*(_format_optdict(kw))))
- def map(self, style, **kw):
+ def map(self, style, query_opt=None, **kw):
"""Query or sets dynamic values of the specified option(s) in
style.
@@ -342,6 +351,11 @@
tuple (usually) containing statespecs grouped in tuples, or list,
or something else of your preference. A statespec is compound of
one or more states and then a value."""
+ if query_opt:
+ query_opt = '' if query_opt.startswith('-') else '-' + query_opt
+ return _list_from_statespec(self.tk.call(self._name, "map", style,
+ query_opt))
+
return _dict_from_tcltuple(self.tk.call(self._name, "map", style,
*(_format_mapdict(kw))))
@@ -353,9 +367,7 @@
or more states. If the default argument is set, it is used as
a fallback value in case no specification for option is found."""
state = ' '.join(state) if state else ''
-
- if not option.startswith('-'):
- option = '-' + option
+ option = '-' if not query_opt.startswith('-') else '' + option
return self.tk.call(self._name, "lookup", style, option, state, default)
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 May 24 22:03:01 2008
@@ -253,19 +253,24 @@
for i in range(len(ttuple) // 2):
opt, val = ttuple[i * 2:i * 2 + 2]
- if hasattr(val, "find") and val.find(' ') != - 1:
- # this could be the padding option
+ if hasattr(val, 'find') and val.find(' ') != -1:
+ # this could be the padding option ("left top right bottom")
val = list(map(int, val.split()))
elif val and hasattr(val, "__len__") and hasattr(val[0], "typename"):
- split_it = lambda ob: getattr(ob, 'typename', None) == 'StateSpec'
- v = [str(v).split() if split_it(v) else v for v in val]
- val = [_flatten(v[i] + [v[i + 1]]) for i in range(0, len(v), 2)]
+ val = _list_from_statespec(val)
opts.append((str(opt)[opt_start:], val))
return dict(opts)
+def _list_from_statespec(stuple):
+ """Construct a list from the given statespec tuple according to the
+ accepted statespec accepted by _format_mapdict."""
+ split_it = lambda ob: getattr(ob, 'typename', None) == 'StateSpec'
+ val = [str(val).split() if split_it(val) else val for val in stuple]
+ return [_flatten(val[i] + [val[i + 1]]) for i in range(0, len(val), 2)]
+
def _list_from_layouttuple(ltuple):
"""Construct a list from the tuple returned by ttk::layout, this is
somewhat the reverse of _format_layoutlist."""
@@ -304,7 +309,7 @@
options = _format_optdict(options)
res = func(*(args + options))
- if len(options) % 2: # option specified without a value
+ if len(options) % 2: # option specified without a value, return its value
return res
return _dict_from_tcltuple(res)
@@ -324,17 +329,21 @@
self.tk = master.tk
- def configure(self, style, **kw):
+ def configure(self, style, query_opt=None, **kw):
"""Query or sets the default value of the specified option(s) in
style.
Each key in kw is an option and each value is either a string or
a sequence identifying the value for that option."""
+ if query_opt:
+ query_opt = '' if query_opt.startswith('-') else '-' + query_opt
+ return self.tk.call(self._name, "configure", style, query_opt)
+
return _dict_from_tcltuple(self.tk.call(self._name, "configure", style,
*(_format_optdict(kw))))
- def map(self, style, **kw):
+ def map(self, style, query_opt=None, **kw):
"""Query or sets dynamic values of the specified option(s) in
style.
@@ -342,6 +351,11 @@
tuple (usually) containing statespecs grouped in tuples, or list,
or something else of your preference. A statespec is compound of
one or more states and then a value."""
+ if query_opt:
+ query_opt = '' if query_opt.startswith('-') else '-' + query_opt
+ return _list_from_statespec(self.tk.call(self._name, "map", style,
+ query_opt))
+
return _dict_from_tcltuple(self.tk.call(self._name, "map", style,
*(_format_mapdict(kw))))
@@ -353,9 +367,7 @@
or more states. If the default argument is set, it is used as
a fallback value in case no specification for option is found."""
state = ' '.join(state) if state else ''
-
- if not option.startswith('-'):
- option = '-' + option
+ option = '-' if not query_opt.startswith('-') else '' + option
return self.tk.call(self._name, "lookup", style, option, state, default)
More information about the Python-checkins
mailing list