Python Tkinter treview:列大小不会扩展到窗口高度

时间:2018-02-11 08:50:41

标签: python tkinter treeview

我无法让Tkinter上的treeview小部件填满整个列。我尝试使用列配置与重量,但它不起作用。当我运行它时,树视图显示它在pic的链接中给出。如何让树视图显示将窗口填充到底部的目录。

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/')

IMAGE

enter image description here
如何使垂直滚动条填充整个窗口高度,而不是仅显示窗口的一部分?

1 个答案:

答案 0 :(得分:0)

所以我设法让滚动条适合整个窗口。这是我的解决方案:

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