tkinter problem with treeview

Discussion in 'Python' started by Wolfgang Meiners, Jun 27, 2011.

  1. 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 += 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
    Wolfgang Meiners, Jun 27, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VHltYm93?=
    Replies:
    2
    Views:
    24,412
    =?Utf-8?B?VHltYm93?=
    Jan 10, 2006
  2. abargaddon
    Replies:
    0
    Views:
    1,036
    abargaddon
    Jan 17, 2008
  3. Per Salmi

    TreeView not rendering nodes? Cacheing of a TreeView

    Per Salmi, Oct 15, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    440
    Steve Drake
    Oct 15, 2003
  4. Ionutz
    Replies:
    2
    Views:
    517
    Ionutz
    Oct 23, 2005
  5. stepkurt

    Microsoft Treeview vs System Treeview

    stepkurt, Aug 29, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    360
    Teemu Keiski
    Sep 2, 2007
Loading...

Share This Page