Python - tkinter: Resize Treeview horizontally

991 Views Asked by At

To illustrate big trees from anytree in tkinter I use Treeview. My aim is to horizontally adjust (dynamically) the size of the column where the data of the Treeview object is stored after resizing the window. I tried to put the Treeview object itself in some Frame and make it with some sticky="ew". For some reason my idea doesn't have any effect.

P.S.: I know I can set the width of the column with

self.treeview.column("# 1",anchor=CENTER, stretch=YES, width=1000)

and the problem in the case of this tree is solved. But I need some dynamical solution for arbitrary trees from anytree.

Hopefully you can help and show me how to solve this problem.

My code:

from tkinter import *
from tkinter import ttk

from anytree import Node, RenderTree, AsciiStyle, PreOrderIter

# anytree definition
udo = Node("Udo the very first king of London and Manchester, born in China and studied in Japan.")
marc = Node("Marc the second monarch of south carolina married Isabel the very first arab princess.", parent=udo)

class AnyTreeView(Toplevel):
    '''
        Illustrates some anytree treeview into some 
    '''
    def __init__(self, master, tree ):  
        super().__init__(master = master)

        self.resizable(width=True, height=False)
        
        self.main_frame = Frame(self)
        self.main_frame.grid(row=1,column=1,sticky="ew")


        self.title('AnyTreeView')
        
        self.tree = tree
        h = len([node.name for node in PreOrderIter(tree)])
        self.treeview = ttk.Treeview(self.main_frame, column=("c1"), height = h)
        self.treeview.column("# 1",anchor=CENTER, stretch=YES)
        self.treeview.pack(expand=True, fill='y')

    def generate(self):
        '''
        Dynamically generates the treeview object with the nodes from the parameter tree.
        '''

        index = 0
        for node in PreOrderIter(self.tree):
            print(node.name)
            self.treeview.insert('',f'{index}', node.name, text = node.name)
            index = index + 1

        for node in PreOrderIter(self.tree):
            for child in node.children:
                self.treeview.move(child.name, node.name, 'end')

def m_tree():
    tv = AnyTreeView(root, udo)
    tv.generate()

root = Tk()

bt = Button(text ="Tree", command = m_tree)
bt.pack()

root.mainloop()

My output: enter image description here

1

There are 1 best solutions below

0
On

Solution based on comments above:

from tkinter import *
from tkinter import ttk

from anytree import Node, RenderTree, AsciiStyle, PreOrderIter

# anytree definition
udo = Node("Udo the very first king of London and Manchester, born in China and studied in Japan.")
marc = Node("Marc the second monarch of south carolina married Isabel the very first arab princess.", parent=udo)

class AnyTreeView(Toplevel):
    '''
        Illustrates some anytree treeview into some 
    '''
    def __init__(self, master, tree ):  
        super().__init__(master = master)

        self.resizable(width=True, height=False)
        
        self.main_frame = Frame(self)
        self.main_frame.grid(row=1,column=1,sticky="ew")


        self.title('AnyTreeView')
        
        self.tree = tree
        h = len([node.name for node in PreOrderIter(tree)])
        self.treeview = ttk.Treeview(self.main_frame, column=("c1"), height = h)
        self.treeview.column("# 1",anchor=CENTER, stretch=YES)


        # Streching treeview after right atjust the window
        self.columnconfigure(1, weight=1)
        self.treeview.pack(expand=True, fill='x')


    def generate(self):
        '''
        Dynamically generates the treeview object with the nodes from the parameter tree.
        '''

        index = 0
        for node in PreOrderIter(self.tree):
            print(node.name)
            self.treeview.insert('',f'{index}', node.name, text = node.name)
            index = index + 1

        for node in PreOrderIter(self.tree):
            for child in node.children:
                self.treeview.move(child.name, node.name, 'end')

def m_tree():
    tv = AnyTreeView(root, udo)
    tv.generate()

root = Tk()

bt = Button(text ="Tree", command = m_tree)
bt.pack()

root.mainloop()