Tkinter Treeview 免费 space 出现
Tkinter Treeview free space appearing
按下简单调用fill_table()
函数的按钮后,table中出现一个空闲的space。为什么会发生这种情况以及如何避免这种情况?
我的代码:
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()
图片:
按下按钮前:
按下按钮后:
似乎如果你 运行 它在启动 mainloop 之后它不会等到函数结束才重新绘制小部件,但是当你创建新列时它会使用默认大小创建它并刷新 window 所以它调整大小 windows。在那之后 width=90
将它改回较小的尺寸但它不会改变 window 的尺寸 - 所以你在 window.
中有空的 space
但是如果我在 width=90
之后使用 table_treeview["show"] = "headings"
那么它不会调整列的大小并且 window 不会改变大小
(在 Linux Mint 上测试)
我对列使用了不同名称的第二个函数,看看它是否会更改列。
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()
按下简单调用fill_table()
函数的按钮后,table中出现一个空闲的space。为什么会发生这种情况以及如何避免这种情况?
我的代码:
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()
图片:
按下按钮前:
按下按钮后:
似乎如果你 运行 它在启动 mainloop 之后它不会等到函数结束才重新绘制小部件,但是当你创建新列时它会使用默认大小创建它并刷新 window 所以它调整大小 windows。在那之后 width=90
将它改回较小的尺寸但它不会改变 window 的尺寸 - 所以你在 window.
但是如果我在 width=90
之后使用 table_treeview["show"] = "headings"
那么它不会调整列的大小并且 window 不会改变大小
(在 Linux Mint 上测试)
我对列使用了不同名称的第二个函数,看看它是否会更改列。
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()