| 1 |
"""Demo based on the demo mclist included with tk source distribution.""" |
| 2 |
import Tkinter |
| 3 |
import tkFont |
| 4 |
import ttk |
| 5 |
|
| 6 |
tree_columns = ("country", "capital", "currency") |
| 7 |
tree_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 |
|
| 25 |
def sortby(tree, col, descending): |
| 26 |
"""Sort tree contents when a column is clicked on.""" |
| 27 |
|
| 28 |
data = [(tree.set(child, col), child) for child in tree.get_children('')] |
| 29 |
|
| 30 |
|
| 31 |
data.sort(reverse=descending) |
| 32 |
for indx, item in enumerate(data): |
| 33 |
tree.move(item[1], '', indx) |
| 34 |
|
| 35 |
|
| 36 |
tree.heading(col, |
| 37 |
command=lambda col=col: sortby(tree, col, int(not descending))) |
| 38 |
|
| 39 |
class 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 |
|
| 62 |
|
| 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 |
|
| 79 |
|
| 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 |
|
| 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 |
|
| 91 |
def 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 |
|
| 106 |
if __name__ == "__main__": |
| 107 |
main() |