Hi all, I have written some helper functions for the tkinter.ttk.treeview widget (using python3, version 3.2). This functions dont work as i expect:
#! /usr/bin/env pyhon3 # -*- encoding: utf-8 -*- from tkinter import * from tkinter.ttk import * # now tkinter widgets get replaced by # tkinter.ttk widgets from tkinter.font import Font def makeTreeview(root, fields, scrollbars=True, show='tree headings', packOptions={'side': RIGHT, 'expand': YES, 'fill': BOTH, 'padx': 2, 'pady': 2}, **extras): """ root: the containing frame fields: [('name', {options})] field[0] is the tree label scrollbars: True: use scrollbars, False: dont use scrollbars show: 'tree' or 'headings' or 'tree headings' """ # define Treeview # fiels[0] belongs to the tree label # fields[1:] belongs to the data values # t is a tuple ('name', {options}) widget=Treeview(root, columns=[t[0] for t in fields[1:]], show=show) # define scrollbars is necessary if scrollbars: ybar=Scrollbar(root) ybar.config(command=widget.yview) ybar.pack(side=RIGHT, expand=YES, fill=Y) # pack ybar first xbar=Scrollbar(root,orient='horizontal') xbar.config(command=widget.xview) xbar.pack(side=BOTTOM, expand=YES, fill=X) # then xbar widget.config(yscrollcommand=ybar.set, xscrollcommand=xbar.set) widget.pack(packOptions) # pack widget last # process the header and the options # of the data values # (n,o) is a tuple consisting of a name and an option i=0 for (n,o) in fields: widget.heading('#%s' % i, text=n) if o: widget.column('#%s' % i, **o) i += 1 if extras: widget.config(**extras) return widget def insert_node_to_treeview(tv, parent, subtree, resize=True, **extras): ''' tv: the treeview to insert the new values parent: the parent node subtree: a tuple (row, children) with row: list of values to insert into this node row[0] belongs to the tree label row[1:] belongs to the data values children: list of children to insert each child is a valid subtree ''' (row, children) = subtree nd=tv.insert(parent, 'end', text=row[0], values=row[1:], **extras) # resize fields if necessary rowsize=[Font().measure(f) for f in row] i=0 for s in rowsize: if tv.column('#%s' %i, option='width') < s: tv.column('#%s' %i, width=rowsize[i]) i += 1 # insert children recursivly if children: for t in children: insert_node_to_treeview(tv, nd, t, resize, **extras) if __name__ == '__main__': class TestMixin(GuiMixin, Frame): def __init__(self, parent=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) longline=\ 'This is a longer line that needs a little more space' MyTree=[(['1', 'Line 1'], [(['1.1', 'Line 1.1'],[]), (['1.2', 'Linee 1.2'], [(['1.2.1', 'Linee 1.2.1'],[]), (['1.2.2', 'Line 1.2.2'],[])])]), (['2', 'Line 2'], [(['2.1', longline], [(['2.1.1', 'This is line 2.1.1'],[])])])] self.tv=makeTreeview(self, fields=[('head', {}), ('zeile 1',{})], scrollbars=True) for t in MyTree: insert_node_to_treeview(self.tv, '', t, open=True) top = self.tv.get_children()[0] self.tv.focus_set() self.tv.focus(top) self.tv.selection_set(top) TestMixin().mainloop() There are some things i dont understand: 1) resizing of the field widths does work in principle, but the resulting field is exactly one charakter to small. 2) I think, resizing should be clearer, but i have no idea, how. 3) The horizontal scrollbar does not work as expected. It does not react on resizing the application window but only on resizing the headers of the fields. Maybe someone can give me some hints. Wolfgang -- http://mail.python.org/mailman/listinfo/python-list