强制 TK 树适应 window/canvas 和滚动条 python 3.9

Forcing a TK tree to fit in a window/canvas with scrollbar python 3.9

我有一个 TK 树视图,其中有很多列,我只是想将它们放入带有水平滚动条的 LabelFrame 中。我遇到过很多类似的问题,但似乎没有任何效果。在每种情况下,树都会与滚动条一起延伸出 window 的边缘。我希望显示的树填充 labelFrame,底部有一个滚动条,允许用户左右滚动以查看所有内容,如果他们的显示器足够大,and/or 调整 window 的大小。为清楚起见,我会说我不在乎 table 有多宽,只要用户可以在默认或调整后的 window.

内左右滚动即可

我不能把我尝试过的每一种排列都放在这里,但这是一个最小的例子。我试过使用和不使用 canvas(虽然我相信我做错了),放入 LabelFrame,pack_propagate(0),订购网格命令等

import tkinter as tk
from tkinter import ttk

#mainWindow is the primary form, this is a secondary window

recentWindow = tk.Tk() #replace the line below to run without mainWindow and add
#mainWindow.mainloop()

#recentWindow = tk.TopLevel(mainWindow)
recentWindow.geometry('800x300')

frmtreeborder = tk.LabelFrame(recentWindow,text='Recent')
recentWindow.pack_propagate(0) #added later for testing, tried with frmtreeborder too
#also tried grid_propagate

#Tried putting the grid statements below for everything but the tree here
#Also tried moving the scrollbar definition here
tree = ttk.Treeview(frmtreeborder,columns=colnames,show='headings',
    height=5,selectmode='extended')

#Populate a colnames just for the example to run
colnames = []
for ii in range(0,20):
    colnames.append(str(ii))
    
for a in colnames:
    tree.column(a,width=85)
    tree.heading(a,text=a)

frmtreeborder.grid(column=0,row=0,sticky='nsew',padx=6,pady=6)
scrollbar = ttk.Scrollbar(recentWindow,orient=tk.HORIZONTAL,command=tree.xview)
tree.configure(xscrollcommand=scrollbar.set)
scrollbar.grid(row=1,column=0,sticky='nw') #if 'new' the scrollbar is the same width as the tree
#Also moved the scrollbar outside the labelFrame to see if that helped
tree.grid(column=0,row=0,sticky='nsew',padx=6,pady=6) #also tried pack

结果window。没有显示的是,如果给滚动条 sticky='nsew' 它与树的宽度相同。回想起来这是有道理的,尽管这意味着所有内容都是动态调整大小的,因为顺序似乎并不重要。我会注意到我有点处于 Monte Carlo 测试制度中,所以我可能在那个过程中混淆了一些东西。

运行 python 3.9.7(蟒蛇)

一些不成功的帖子:

Python Treeview scrollbar

tkinter ttk treeview...

您需要在 recentWindowfrmtreeborder 上调用 rowconfigure(0, weight=1)columnconfigure(0, weight=1),这样 frmtreebordertree 的大小是受其父容器大小的约束。

在滚动条上也使用 sticky='ew' 而不是 sticky='nw'

import tkinter as tk
from tkinter import ttk

recentWindow = tk.Tk()
recentWindow.geometry('800x300')

recentWindow.columnconfigure(0, weight=1)
recentWindow.rowconfigure(0, weight=1)

frmtreeborder = tk.LabelFrame(recentWindow,text='Recent')

frmtreeborder.columnconfigure(0, weight=1)
frmtreeborder.rowconfigure(0, weight=1)

colnames = [str(ii) for ii in range(20)]
tree = ttk.Treeview(frmtreeborder,columns=colnames,show='headings',
                    height=5,selectmode='extended')

for a in colnames:
    tree.column(a,width=85)
    tree.heading(a,text=a)

scrollbar = ttk.Scrollbar(recentWindow,orient=tk.HORIZONTAL,command=tree.xview)
tree.configure(xscrollcommand=scrollbar.set)

frmtreeborder.grid(column=0,row=0,sticky='nsew',padx=6,pady=6)
tree.grid(column=0,row=0,sticky='nsew',padx=6,pady=6)
scrollbar.grid(row=1,column=0,sticky='ew')

recentWindow.mainloop()

结果: