1
"""Demo based on the demo mclist included with tk source distribution."""
4
from tkinter import ttk
6
tree_columns = ("country", "capital", "currency")
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")
25
def sortby(tree, col, descending):
26
"""Sort tree contents when a column is clicked on."""
28
data = [(tree.set(child, col), child) for child in tree.get_children('')]
31
data.sort(reverse=descending)
32
for indx, item in enumerate(data):
33
tree.move(item[1], '', indx)
35
# switch the heading so that it will sort in the opposite direction
37
command=lambda col=col: sortby(tree, col, int(not descending)))
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."))
58
container = ttk.Frame()
59
container.pack(fill='both', expand=True)
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)
71
container.grid_columnconfigure(0, weight=1)
72
container.grid_rowconfigure(0, weight=1)
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
80
self.tree.column(col, width=tkinter.font.Font().measure(col.title()))
82
for item in tree_data:
83
self.tree.insert('', 'end', values=item)
85
# adjust columns lenghts if necessary
86
for indx, val in enumerate(item):
87
ilen = tkinter.font.Font().measure(val)
88
if self.tree.column(tree_columns[indx], width=None) < ilen:
89
self.tree.column(tree_columns[indx], width=ilen)
93
root.wm_title("Multi-Column List")
94
root.wm_iconname("mclist")
98
plastik_theme.install('~/tile-themes/plastik/plastik')
101
warnings.warn("plastik theme being used without images")
106
if __name__ == "__main__":