将树视图粘贴到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()

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)

您只需要做两件事:

  • 配置leftFrame以将可用垂直空间分配给放置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以将可用垂直空间分配给放置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
...

相关内容

  • 没有找到相关文章

最新更新