whmsft
whmsft

Reputation: 477

Sticking Treeview to N+S

I want to expand my tk treeview to north-south, but I can't do it. Here's my try(code):

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()

The class PathView creates a treeview that shows the path and all files that it has, but I am not able to stick it to north and south. What am I doing wrong here?

Upvotes: 1

Views: 99

Answers (2)

acw1668
acw1668

Reputation: 46678

You just need to do two things:

  • config leftFrame to allocate available vertical space to row 0 in which PathView.frame is put:
...
leftFrame = tk.Frame(splitter, bg='red')
rightFrame = tk.Frame(splitter, bg='blue')
leftFrame.rowconfigure(0, weight=1) # let PathView.frame use all vertical space
...
  • config PathView.frame to allocate available vertical space to row 0 in which PathView.tree is put:
class PathView:
    def __init__(self, master, path):
        ...
        frame = tk.Frame(master)
        frame.rowconfigure(0, weight=1) # let self.tree use all vertical space
        ...

Upvotes: 1

user15801675
user15801675

Reputation:

.pack() is better here:

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)

Upvotes: 2

Related Questions