[Python-checkins] r62625 - sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py
guilherme.polo
python-checkins at python.org
Fri May 2 04:00:13 CEST 2008
Author: guilherme.polo
Date: Fri May 2 04:00:13 2008
New Revision: 62625
Log:
Wrapped Ttk Notebook.
Moved code for breaking a tuple in pairs and generating a dict from it
to a separated function, so the method tab at Notebook can benefit from
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 Fri May 2 04:00:13 2008
@@ -80,6 +80,15 @@
return Tkinter._flatten(opts)
+def _dict_from_tcltuple(t):
+ """Break tuple in pairs then build the return dict.
+
+ t is expected to contain an even number of elements."""
+ opts = (t[i * 2:i * 2 + 2] for i in range(len(t) / 2))
+
+ # note that the '-' prefixing options will be removed
+ return dict((str(opt[0])[1:], opt[1]) for opt in opts)
+
class Style(object):
"""Manipulate style database."""
@@ -468,7 +477,134 @@
Widget.__init__(self, master, "ttk::menubutton", cnf, kw)
-class Notebook(Widget): pass
+class Notebook(Widget):
+ """Ttk Notebook widget manages a collection of windows and displays
+ a single one at a time. Each child window is associated with a tab,
+ which the user may select to change the currently-displayed window."""
+
+ def __init__(self, master=None, cnf={}, **kw):
+ """Construct a Ttk Notebook with parent master.
+
+ STANDARD OPTIONS
+
+ class, cursor, style, takefocus
+
+ WIDGET-SPECIFIC OPTIONS
+
+ height, padding, width
+
+ TAB OPTIONS
+
+ state, sticky, padding, text, image, compound, underline
+
+ TAB IDENTIFIERS (tab_id)
+
+ The tab_id argument found in several methods may take any of
+ the following forms:
+
+ * An integer between zero and the number of tabs
+ * The name of a child window
+ * A positional specification of the form "@x.y", which
+ defines the tab
+ * The string "current", which identifies the
+ currently-selected tab
+ * The string "end", which returns the number of tabs (only
+ valis for method index)
+ """
+ Widget.__init__(self, master, "ttk::notebook", cnf, kw)
+
+
+ def add(self, child, **kw):
+ """Adds a new tab to the notebook.
+
+ If window is currently managed by the notebook but hidden, it is
+ restored to its previous position."""
+ self.tk.call(self._w, "add", child, *(_format_optdict(kw)))
+
+
+ def forget(self, tab_id):
+ """Removes the tab specified by tab_id, unmaps and unmanages the
+ associated window."""
+ self.tk.call(self._w, "forget", tab_id)
+
+
+ def hide(self, tab_id):
+ """Hides the tab specified by tab_id.
+
+ The tab will not be displayed, but the associated window remains
+ managed by the notebook and its configuration remembered. Hidden
+ tabs may be restored with the add command."""
+ self.tk.call(self._w, "hide", tab_id)
+
+
+ def index(self, tab_id):
+ """Returns the numeric index of the tab specified by tab_id, or
+ the total number of tabs if tab_id is the string "end"."""
+ return self.tk.call(self._w, "index", tab_id)
+
+
+ def insert(self, pos, child, **kw):
+ """Inserts a pane at the specified position.
+
+ pos is either the string end, an integer index, or the name of
+ a managed child. If child is already managed by the notebook,
+ moves it to the specified positon."""
+ self.tk.call(self._w, "insert", pos, child, *(_format_optdict(kw)))
+
+
+ def select(self, tab_id=None):
+ """Selects the specified tab.
+
+ The associated child window will be displayed, and the
+ previously-selected window (if different) is unmapped. If tab_id
+ is omitted, returns the widget name of the currently selected
+ pane."""
+ return self.tk.call(self._w, "select", tab_id)
+
+
+ def tab(self, tab_id, **kw):
+ """Query or modify the options of the specific tab.
+
+ If no option is specified, returns a dictionary of the tab option
+ values. If one option is specified, returns the value of that
+ option. Otherwise, sets the options to the corresponding values."""
+ opts = _format_optdict(kw)
+ res = self.tk.call(self._w, "tab", tab_id, *opts)
+
+ if len(opts) % 2: # option specified without a value
+ return res
+
+ return _dict_from_tcltuple(res)
+
+
+ def tabs(self):
+ """Returns a list of windows managed by the notebook."""
+ return self.tk.call(self._w, "tabs")
+
+
+ def enableTraversal(self):
+ """Enable keyboard traversal for a toplevel window containing
+ this notebook.
+
+ This will extend the bindings for the toplevel window containing
+ this notebook as follows:
+
+ Control-Tab: selects the tab following the currently selected
+ one
+
+ Shift-Control-Tab: selects the tab preceding the currently
+ selected one
+
+ Alt-K: where K is the mnenomic (underlined) character of any
+ tab, will select that tab.
+
+ Multiple notebooks in a single toplevel may be enabled for
+ traversal, including nested notebooks. However, notebook traversal
+ only works properly if all panes are direct children of the
+ notebook."""
+ # XXX I didn't notice any difference after calling this, maybe some
+ # WM will benefit from this ?
+ self.tk.call("ttk::notebook::enableTraversal", self)
class Panedwindow(Widget, Tkinter.PanedWindow):
@@ -518,9 +654,7 @@
if len(opts) % 2: # option specified without a value
return res
- # break tuple in pairs then build the return dict
- opts = (res[i * 2:i * 2 + 2] for i in range(len(res) / 2))
- return dict((str(opt[0])[1:], opt[1]) for opt in opts)
+ return _dict_from_tcltuple(res)
def sashpos(self, index, newpos=None):
More information about the Python-checkins
mailing list