• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Demo based on the demo mclist included with tk source distribution."""
2import Tkinter
3import tkFont
4import ttk
5
6tree_columns = ("country", "capital", "currency")
7tree_data = [
8    ("Argentina",      "Buenos Aires",     "ARS"),
9    ("Australia",      "Canberra",         "AUD"),
10    ("Brazil",         "Brazilia",         "BRL"),
11    ("Canada",         "Ottawa",           "CAD"),
12    ("China",          "Beijing",          "CNY"),
13    ("France",         "Paris",            "EUR"),
14    ("Germany",        "Berlin",           "EUR"),
15    ("India",          "New Delhi",        "INR"),
16    ("Italy",          "Rome",             "EUR"),
17    ("Japan",          "Tokyo",            "JPY"),
18    ("Mexico",         "Mexico City",      "MXN"),
19    ("Russia",         "Moscow",           "RUB"),
20    ("South Africa",   "Pretoria",         "ZAR"),
21    ("United Kingdom", "London",           "GBP"),
22    ("United States",  "Washington, D.C.", "USD")
23    ]
24
25def sortby(tree, col, descending):
26    """Sort tree contents when a column is clicked on."""
27    # grab values to sort
28    data = [(tree.set(child, col), child) for child in tree.get_children('')]
29
30    # reorder data
31    data.sort(reverse=descending)
32    for indx, item in enumerate(data):
33        tree.move(item[1], '', indx)
34
35    # switch the heading so that it will sort in the opposite direction
36    tree.heading(col,
37        command=lambda col=col: sortby(tree, col, int(not descending)))
38
39class App(object):
40    def __init__(self):
41        self.tree = None
42        self._setup_widgets()
43        self._build_tree()
44
45    def _setup_widgets(self):
46        msg = ttk.Label(wraplength="4i", justify="left", anchor="n",
47            padding=(10, 2, 10, 6),
48            text=("Ttk is the new Tk themed widget set. One of the widgets it "
49                  "includes is a tree widget, which can be configured to "
50                  "display multiple columns of informational data without "
51                  "displaying the tree itself. This is a simple way to build "
52                  "a listbox that has multiple columns. Clicking on the "
53                  "heading for a column will sort the data by that column. "
54                  "You can also change the width of the columns by dragging "
55                  "the boundary between them."))
56        msg.pack(fill='x')
57
58        container = ttk.Frame()
59        container.pack(fill='both', expand=True)
60
61        # XXX Sounds like a good support class would be one for constructing
62        #     a treeview with scrollbars.
63        self.tree = ttk.Treeview(columns=tree_columns, show="headings")
64        vsb = ttk.Scrollbar(orient="vertical", command=self.tree.yview)
65        hsb = ttk.Scrollbar(orient="horizontal", command=self.tree.xview)
66        self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
67        self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
68        vsb.grid(column=1, row=0, sticky='ns', in_=container)
69        hsb.grid(column=0, row=1, sticky='ew', in_=container)
70
71        container.grid_columnconfigure(0, weight=1)
72        container.grid_rowconfigure(0, weight=1)
73
74    def _build_tree(self):
75        for col in tree_columns:
76            self.tree.heading(col, text=col.title(),
77                command=lambda c=col: sortby(self.tree, c, 0))
78            # XXX tkFont.Font().measure expected args are incorrect according
79            #     to the Tk docs
80            self.tree.column(col, width=tkFont.Font().measure(col.title()))
81
82        for item in tree_data:
83            self.tree.insert('', 'end', values=item)
84
85            # adjust columns lenghts if necessary
86            for indx, val in enumerate(item):
87                ilen = tkFont.Font().measure(val)
88                if self.tree.column(tree_columns[indx], width=None) < ilen:
89                    self.tree.column(tree_columns[indx], width=ilen)
90
91def main():
92    root = Tkinter.Tk()
93    root.wm_title("Multi-Column List")
94    root.wm_iconname("mclist")
95
96    import plastik_theme
97    try:
98        plastik_theme.install('~/tile-themes/plastik/plastik')
99    except Exception:
100        import warnings
101        warnings.warn("plastik theme being used without images")
102
103    app = App()
104    root.mainloop()
105
106if __name__ == "__main__":
107    main()
108