Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tkinter problem with treeview

Reply
Thread Tools

tkinter problem with treeview

 
 
Wolfgang Meiners
Guest
Posts: n/a
 
      06-27-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to access the Page from a TreeNode (treeview 's element) : addinga context menu to a treeview abargaddon ASP .Net Building Controls 0 01-17-2008 11:05 AM
Microsoft Treeview vs System Treeview stepkurt ASP .Net Web Controls 1 09-02-2007 11:32 AM
ASP.NET 2.0 TreeView - Programatically Selecting A TreeView Node =?Utf-8?B?VHltYm93?= ASP .Net 2 01-10-2006 03:51 AM
Issue with a custom TreeView derived from the TreeView control Ionutz ASP .Net Web Controls 2 10-23-2005 03:23 PM
TreeView not rendering nodes? Cacheing of a TreeView Per Salmi ASP .Net Web Controls 1 10-15-2003 01:15 PM



Advertisments