我读到,你可以在 .pack() 布局管理器中将滚动条放在左侧,但我找不到 .grid() 布局管理器的有效解决方案。我的书中的描述说我应该使用它sticky = 'nsw'
来定位。然后我尝试将其移到左侧,请参阅我的示例。
这没有任何效果:
import tkinter as tk
from tkinter import ttk
from pathlib import Path
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
paths = Path('.').glob('**/*')
tv = ttk.Treeview(root, columns=['size','modified'], selectmode=None)
tv.heading('#0', text='Name')
tv.heading('size', text='Size', anchor='center')
tv.heading('modified', text='Modifies', anchor='center')
tv.column('#0', stretch = True, anchor='w')
tv.column('size', width=100, anchor='center')
tv.column('modified',anchor='center')
tv.grid_rowconfigure(0, weight=1)
tv.grid_columnconfigure(0, weight=1)
tv.grid(row=0, column=0, sticky='nsew')
#tv.pack(expand=True, fill='both')
for path in paths:
meta = path.stat()
parent = str(path.parent)
if parent == '.':
parent = ''
tv.insert(parent, 'end', iid=str(path), text=str(path.name), values=[meta.st_size, meta.st_mtime])
scrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL, command=tv.yview)
tv.configure(yscrollcommand=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='nse') #scrollbar goes not to the left!
root.mainloop()
在树形视图中是否无法将滚动条定位到窗口的左侧?
如果你
tv
将 的列改为 1,scrollbar
将 的列改为 0,它就会按照你想要的方式运行