Skip to content Skip to sidebar Skip to footer

How To Get A Treeview Columns To Fit The Frame It Is Within

When dynamically changing a treeviews data, I want the rows to expand to the size of the treeview's frame. Currently if I make the GUI fullscreen and re-fill the data it only uses

Solution 1:

The issue is that the columns are created with an initial size (default is 200 pixels I think) and only stretch if the treeview is resized after their creation. So you will have to manually set the column width to occupy the whole available space. To do so you can use the width argument of the column() method of the treeview:

self.tree.column(col, width=col_width)

where col_width is the total width divided by the number of columns. Incorporating this code in the fill() function gives

deffill(self):

    if self.has_data():
        self.tree.delete(*self.tree.get_children())

    i = random.randrange(1,10)
    self.tree["columns"]=tuple([str(i) for i inrange(i)])

    col_width = self.tree.winfo_width() // i # compute the width of one columnfor col in self.tree['columns']:
        self.tree.heading(col, text="Column {}".format(col), anchor=tk.CENTER)
        self.tree.column(col, anchor=tk.CENTER, width=col_width)  # set column width

    j = random.randrange(10)

    for j inrange(j):

        self.tree.insert("", "end", values = tuple([k for k inrange(i)]))

Post a Comment for "How To Get A Treeview Columns To Fit The Frame It Is Within"