我正在嘗試在樹視圖中插入垂直滾動條,使其顯示在樹視圖的列標簽下,而不是在標簽旁邊/旁邊。我已經嘗試在滾動條小部件中添加 pady,但仍然沒有將它放在列標簽下(只是從頂部創建一個偏移量)。非常感謝任何幫助(查看您的@Bryan Oakley)。我嘗試了許多填充技術來使垂直滾動條從列標簽下方開始,但到目前為止沒有任何效果。這是一個最小的作業代碼:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry('400x150')
treeFrame = Frame(root)
treeFrame.grid(row=0, column=0, sticky='nsew')
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
treeFrame.rowconfigure(0, weight=1)
treeFrame.columnconfigure(0, weight=1)
tree=ttk.Treeview(treeFrame, selectmode='browse')
tree.grid(row=0, column=0, sticky='nsew')
tree['columns']='LastName'
tree.column('#0', width=50)
tree.column('LastName', width=50)
tree.heading('#0', text='FirstName', anchor='w')
tree.heading('LastName', text='LastName', anchor='w')
tree.insert(parent='', index='end', iid=0, text='Jack', values=('Sparrow'))
tree.insert(parent='', index='end', iid=1, text='Harry', values=('Potter'))
tree.insert(parent='', index='end', iid=3, text='Jack1', values=('Sparrow'))
tree.insert(parent='', index='end', iid=4, text='Harry1', values=('Potter'))
tree.insert(parent='', index='end', iid=5, text='Jack2', values=('Sparrow'))
tree.insert(parent='', index='end', iid=6, text='Harry2', values=('Potter'))
tree.insert(parent='', index='end', iid=7, text='Jack3', values=('Sparrow'))
tree.insert(parent='', index='end', iid=8, text='Harry3', values=('Potter'))
scrollY = Scrollbar(treeFrame, orient='vertical', command=tree.yview)
scrollY.grid(row=0, column=1, sticky='ns')
tree['yscrollcommand'] = scrollY.set
s= ttk.Style()
s.theme_use('clam')
root.mainloop()
uj5u.com熱心網友回復:
您可以將滾動條放在單元格的右側tree:
...
s = ttk.Style()
s.theme_use('clam')
tree.update() # make sure tree is updated
bbox = tree.bbox(0) # get bounding box of first row (iid=0)
# bbox[1] will be the top y of first row
scrollY.grid(row=0, column=0, sticky='nse', pady=(bbox[1],2), padx=(0,2))
root.mainloop()
請注意,它僅在插入至少一行時才有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422090.html
標籤:
