我有一個 TK 樹視圖,它有很多列,我只是希望將它們放入帶有水平滾動條的 LabelFrame 中。我經歷了很多類似的問題,但似乎沒有任何效果。在每種情況下,樹都與滾動條一起延伸到視窗邊緣。我希望顯示的樹填充 labelFrame,底部有一個滾動條,允許用戶左右滾動以查看所有內容,和/或如果他們的監視器足夠大,則調整視窗大小。為了清楚起見,我會說我不在乎表格有多寬,只要用戶可以在默認視窗或調整大小的視窗內左右滾動即可。
我不能把我嘗試過的每一個排列都放在這里,但這是一個最小的例子。我嘗試過使用和不使用畫布(盡管我認為我做錯了),放入 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
結果視窗。沒有顯示的是,如果滾動條被賦予sticky='nsew',它與樹的寬度相同。回想起來,這是有道理的,盡管這意味著一切都是動態調整的,因為順序似乎并不重要。我會注意到我有點處于蒙特卡洛測驗制度中,所以我可能在那個程序中弄混了一些東西。
運行 python 3.9.7 (Anaconda)
一些不成功的執行緒:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431681.html
