[Python-checkins] r63217 - in sandbox/trunk/ttk-gsoc: Lib/lib-tk/Ttk.py Lib/tkinter/ttk.py samples/treeview_multicolumn.py

guilherme.polo python-checkins at python.org
Wed May 14 04:03:08 CEST 2008


Author: guilherme.polo
Date: Wed May 14 04:03:07 2008
New Revision: 63217

Log:
New sample added for demonstrating treeview some more;
Updated method insert of Treeview so it correctly deals with items
containing spaces.


Added:
   sandbox/trunk/ttk-gsoc/samples/treeview_multicolumn.py
Modified:
   sandbox/trunk/ttk-gsoc/Lib/lib-tk/Ttk.py
   sandbox/trunk/ttk-gsoc/Lib/tkinter/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	Wed May 14 04:03:07 2008
@@ -1145,12 +1145,16 @@
         is specified, it is used as the item identifier, iid must not 
         already exist in the tree. Otherwise, a new unique identifier
         is generated."""
+        opts, values = _format_optdict(kw, ignore='values'), kw.get('values')
+        # values may need special formatting if any value contain a space
+        if values: # XXX maybe this could fit into _format_optdict
+            opts += ("-values", 
+                ' '.join(('{%s}' if ' ' in v else '%s') % v for v in values))
         if iid:
             res = self.tk.call(self._w, "insert", parent, index, "-id", iid, 
-                               *(_format_optdict(kw)))
+                               *opts)
         else:
-            res = self.tk.call(self._w, "insert", parent, index, 
-                               *(_format_optdict(kw)))
+            res = self.tk.call(self._w, "insert", parent, index, *opts)
 
         return res
 

Modified: sandbox/trunk/ttk-gsoc/Lib/tkinter/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/Lib/tkinter/ttk.py	(original)
+++ sandbox/trunk/ttk-gsoc/Lib/tkinter/ttk.py	Wed May 14 04:03:07 2008
@@ -1145,12 +1145,16 @@
         is specified, it is used as the item identifier, iid must not 
         already exist in the tree. Otherwise, a new unique identifier
         is generated."""
+        opts, values = _format_optdict(kw, ignore='values'), kw.get('values')
+        # values may need special formatting if any value contain a space
+        if values: # XXX maybe this could fit into _format_optdict
+            opts += ("-values",
+                ' '.join(('{%s}' if ' ' in v else '%s') % v for v in values))
         if iid:
             res = self.tk.call(self._w, "insert", parent, index, "-id", iid, 
-                               *(_format_optdict(kw)))
+                               *opts)
         else:
-            res = self.tk.call(self._w, "insert", parent, index, 
-                               *(_format_optdict(kw)))
+            res = self.tk.call(self._w, "insert", parent, index, *opts)
 
         return res
 

Added: sandbox/trunk/ttk-gsoc/samples/treeview_multicolumn.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/ttk-gsoc/samples/treeview_multicolumn.py	Wed May 14 04:03:07 2008
@@ -0,0 +1,110 @@
+"""Demo based on the demo mclist.tcl included with tk source distribution."""
+import sys
+import Tkinter
+import tkFont
+
+if sys.version_info[0] > 2:
+    from tkinter import ttk
+else:
+    import Ttk as ttk
+
+tree_columns = ("country", "capital", "currency")
+tree_data = [
+    ("Argentina",      "Buenos Aires",     "ARS"),
+    ("Australia",      "Canberra",         "AUD"),
+    ("Brazil",         "Brazilia",         "BRL"),
+    ("Canada",         "Ottawa",           "CAD"),
+    ("China",          "Beijing",          "CNY"),
+    ("France",         "Paris",            "EUR"),
+    ("Germany",        "Berlin",           "EUR"),
+    ("India",          "New Delhi",        "INR"),
+    ("Italy",          "Rome",             "EUR"),
+    ("Japan",          "Tokyo",            "JPY"),
+    ("Mexico",         "Mexico City",      "MXN"),
+    ("Russia",         "Moscow",           "RUB"),
+    ("South Africa",   "Pretoria",         "ZAR"),
+    ("United Kingdom", "London",           "GBP"),
+    ("United States",  "Washington, D.C.", "USD")
+    ]
+
+def sortby(tree, col, descending):
+    """Sort tree contents when a column is clicked on."""
+    # grab values to sort
+    data = [(tree.set(child, col), child) for child in tree.get_children('')]
+
+    # reorder data
+    data.sort(reverse=descending)
+    for indx, item in enumerate(data):
+        tree.move(item[1], '', indx)
+
+    # switch the heading so that it will sort in the opposite direction
+    tree.heading(col, command=tree.register(
+        lambda col=col: sortby(tree, col, int(not descending))))
+
+class App(object):
+    def __init__(self):
+        self.tree = None   
+        self._setup_widgets()
+        self._build_tree()
+    
+    def _setup_widgets(self):
+        msg = ttk.Label(wraplength="4i", justify="left", anchor="n", 
+            # XXX Change the following to a list, run it using tk 8.4 and 
+            #     watch it segfault.
+            padding=(10, 2, 10, 6), 
+            text=("Ttk is the new Tk themed widget set. One of the widgets it "
+                  "includes is a tree widget, which can be configured to "
+                  "display multiple columns of informational data without "
+                  "displaying the tree itself. This is a simple way to build "
+                  "a listbox that has multiple columns. Clicking on the "
+                  "heading for a column will sort the data by that column. "
+                  "You can also change the width of the columns by dragging "
+                  "the boundary between them."))
+        msg.pack(fill='x')
+
+        container = ttk.Frame()
+        container.pack(fill='both', expand=True)
+
+        # XXX Sounds like a good support class would be one for constructing
+        #     a treeview with scrollbars.
+        self.tree = ttk.Treeview(columns=tree_columns, show="headings")
+        vsb = Tkinter.Scrollbar(orient="vertical", command=self.tree.yview)
+        hsb = Tkinter.Scrollbar(orient="horizontal", command=self.tree.xview)
+        self.tree.configure(yscroll=vsb.set, xscroll=hsb.set)
+        self.tree.grid_configure(**{'in': container})
+        self.tree.grid(column=0, row=0, sticky='nsew')
+        vsb.grid_configure(**{'in': container})
+        vsb.grid(column=1, row=0, sticky='ns')
+        hsb.grid_configure(**{'in': container})
+        hsb.grid(column=0, row=1, sticky='ew')
+
+        container.grid_columnconfigure(0, weight=1)
+        container.grid_rowconfigure(0, weight=1)
+
+    def _build_tree(self):
+        for col in tree_columns:
+            self.tree.heading(col, text=col.title(), 
+                command=self.tree.register(
+                    lambda c=col: sortby(self.tree, c, 0)))
+            # XXX tkFont.Font().measure expected args are incorrect according 
+            #     to the Tk docs
+            self.tree.column(col, width=tkFont.Font().measure(col.title()))
+
+        for item in tree_data:
+            self.tree.insert('', 'end', values=item)
+
+            # adjust columns lenghts if necessary
+            for indx, val in enumerate(item):
+                ilen = tkFont.Font().measure(val)
+                if self.tree.column(tree_columns[indx], width=None) < ilen:
+                    self.tree.column(tree_columns[indx], width=ilen)
+    
+def main():
+    root = Tkinter.Tk()
+    root.wm_title("Multi-Column List")
+    root.wm_iconname("mclist")
+    app = App()
+    root.mainloop()
+
+if __name__ == "__main__":
+    main()


More information about the Python-checkins mailing list