坚持 Treeview 到 N+S

Sticking Treeview to N+S

我想将我的 tk treeview 扩展到 north-south,但我做不到。 这是我的尝试(代码):

import os
from tkinter import *
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
class PathView(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(yscroll=ysb.set, xscroll=xsb.set)
        self.tree.heading('#0', text='Project tree', anchor='w')
        self.tree.grid(sticky = N+S+E+W)
        ysb.grid(row=0, column=1, sticky='ns')
        xsb.grid(row=1, column=0, sticky='ew')
        frame.grid(sticky=N+S)
        abspath = os.path.abspath(path)
        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))
splitter = tk.PanedWindow(root, handlesize=2, orient=tk.HORIZONTAL)
splitter.pack(fill='both', expand=True)

leftFrame = tk.Frame(splitter, bg='red')
rightFrame = tk.Frame(splitter, bg='blue')
framed = PathView(leftFrame, path="E:/")
splitter.add(leftFrame, width=100)
splitter.add(rightFrame)
root.state("zoomed")
root.mainloop()

class PathView 创建了一个 treeview 来显示路径和它拥有的所有文件,但我无法将其粘贴到 north and south。 我在这里做错了什么?

.pack() 在这里更好:

ysb.pack(side=RIGHT,fill=Y)
xsb.pack(side=BOTTOM,fill=X)
self.tree.pack(fill=BOTH,expand=1)
       
frame.pack(fill=BOTH,expand=1)

你只需要做两件事:

  • config leftFrame 将可用的垂直 space 分配给放置 PathView.frame 的第 0 行:
...
leftFrame = tk.Frame(splitter, bg='red')
rightFrame = tk.Frame(splitter, bg='blue')
leftFrame.rowconfigure(0, weight=1) # let PathView.frame use all vertical space
...
  • 配置 PathView.frame 将可用的垂直 space 分配给放置 PathView.tree 的第 0 行:
class PathView:
    def __init__(self, master, path):
        ...
        frame = tk.Frame(master)
        frame.rowconfigure(0, weight=1) # let self.tree use all vertical space
        ...