![](/img/trans.png)
[英]Tkinter: How to get frame in canvas window to expand to the size of the canvas?
[英]Python Tkinter treview: column size does not expand to window height
我无法使Tkinter上的treeview小部件填充整列。 我尝试使用带有权重的列配置,但是它不起作用。 当我运行它时,树视图将显示它,如图片链接中所给。 我如何获取树视图以显示目录,这些目录将窗口填满到底部。
class dir_tree(object):
def __init__(self, master, path):
self.nodes = dict()
frame = tk.Frame(master)
self.tree = ttk.Treeview(frame)
ysb = ttk.Scrollbar(frame, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(frame,orient='horizontal',command=self.tree.xview)
self.tree.configure( xscroll=master.winfo_screenheight())
self.tree.heading('#0', text='Manga Directory', anchor='w')
self.tree.grid(column = 0, sticky = 'ns')
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
frame.grid(column = 0, sticky = 'ns')
self.tree.columnconfigure(0, weight=1)
abspath = os.path.abspath('/home/user/Desktop/')
self.insert_node('', abspath, abspath)
self.tree.bind('<<TreeviewOpen>>', self.open_node)
def insert_node(self, parent, text, abspath):
node = self.tree.insert(parent, 'end', text=text, open=False)
if os.path.isdir(abspath):
self.nodes[node] = abspath
self.tree.insert(node, 'end')
def open_node(self, event):
node = self.tree.focus()
abspath = self.nodes.pop(node, None)
if abspath:
self.tree.delete(self.tree.get_children(node))
for p in os.listdir(abspath):
self.insert_node(node, p, os.path.join(abspath, p))
if __name__ == "__main__":
dirr = dir_tree(root, path='/home/user/Desktop/')
因此,我设法使滚动条适合整个窗口。 这是我的解决方案:
class dir_tree(object):
def __init__(self, master, path):
self.nodes = dict()
notebook = ttk.Notebook(master, width = 2000)
self.tree = ttk.Treeview(notebook)
ysb = ttk.Scrollbar(notebook, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(notebook, orient='horizontal',command=self.tree.xview)
self.tree.configure(yscrollcommand=ysb.set, xscrollcommand=xsb.set)
self.tree.heading('#0', text='Manga Directory', anchor='w')
xsb.pack(side=tk.BOTTOM, fill=tk.X)
ysb.pack(side=tk.RIGHT, fill=tk.Y)
notebook.pack(side = LEFT,fill = tk.Y)
self.tree.pack(side = LEFT, fill = tk.BOTH, expand = True)
abspath = os.path.abspath('/home/user/Desktop/')
self.insert_node('', abspath, abspath)
self.tree.bind('<<TreeviewOpen>>', self.open_node)
def insert_node(self, parent, text, abspath):
node = self.tree.insert(parent, 'end', text=text, open=False)
if os.path.isdir(abspath):
self.nodes[node] = abspath
self.tree.insert(node, 'end')
def open_node(self, event):
node = self.tree.focus()
abspath = self.nodes.pop(node, None)
if abspath:
self.tree.delete(self.tree.get_children(node))
for p in os.listdir(abspath):
self.insert_node(node, p, os.path.join(abspath, p))
if __name__ == "__main__":
dirr = dir_tree(root, path='/home/user/Desktop/') # Path to Downloads
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.