max_slkk
max_slkk

Reputation: 3

Tkinter Treeview free space appearing

After pressing the button that simply calls the fill_table() function, a free space appears in the table. Why that happens and how to avoid that? My code:

import tkinter as tk
import tkinter.ttk as ttk


def fill_table():
    required_columns = ['col1', 'col2', 'col3']
    table_treeview["columns"] = required_columns
    table_treeview["show"] = "headings"
    for col in required_columns:
        table_treeview.column(col, width=90)
        table_treeview.heading(col, text=col)


root = tk.Tk()

table_treeview = ttk.Treeview(root)
table_treeview.pack()

button = tk.Button(root, text='Restart', command=fill_table)
button.pack()

fill_table()


root.mainloop()

Images:

Upvotes: 0

Views: 1273

Answers (1)

furas
furas

Reputation: 142661

It seems that if you run it after starting mainloop then it doesn't wait to the end of function to redraw widget but when you create new column then it create it with default size and refresh window so it resizes windows. After that width=90 change it back to smaller size but it doesn't change window's size - so you have empty space in window.

But if I use table_treeview["show"] = "headings" after width=90 then it doesn't resize columns and window doesn't change size

(Tested on Linux Mint)


I used second function with different names for columns to see if it readlly change columns.

import tkinter as tk
import tkinter.ttk as ttk


def fill_table():
    required_columns = ['col1', 'col2', 'col3']
    table_treeview["columns"] = required_columns
    for col in required_columns:
        table_treeview.column(col, width=90)
        table_treeview.heading(col, text=col)
    table_treeview["show"] = "headings"  # use after setting column's size

def fill_table_2():
    required_columns = ['colA', 'colB', 'colC']
    table_treeview["columns"] = required_columns
    for col in required_columns:
        table_treeview.column(col, width=90)
        table_treeview.heading(col, text=col)
    table_treeview["show"] = "headings"  # use after setting column's size

root = tk.Tk()

table_treeview = ttk.Treeview(root)
table_treeview.pack()

button = tk.Button(root, text='Restart', command=fill_table_2)
button.pack()

fill_table()

root.mainloop()

Upvotes: 1

Related Questions