Jung-suk
Jung-suk

Reputation: 172

Resizing the treeview manually in tkinter

Here i'm trying to drag the sample1 treeview to downward and upward manually(maximizing and minimizing the number of visible rows). As i drag the 1st treeview downward manually, the 1st treeview need to extend and the 2nd treeview needs to minimize. Can we do that in Tkinter. Please help me with this

Sample code:

import tkinter as tk
import tkinter.ttk as ttk


class Window:
    def __init__(self, master):
        self.master = master
        self.master.geometry('630x500+90+30')
        
        self.button = tk.Button(self.master,height=1,width=11, text="Sample").place(x=0,y=0)
        self.label = tk.Label(self.master, text='sample1 ',font=("Algerian", 20,'bold')).grid(row=0,columnspan=3)
        
        cols = ('aa','bb')
        self.treeview = ttk.Treeview(self.master, columns=cols)
        v_scrollbar = ttk.Scrollbar(self.master, orient='vertical', command=self.treeview.yview)
        self.treeview.config( yscrollcommand=v_scrollbar.set)
        for col in cols:
            self.treeview.heading(col, text=col)
        self.treeview.grid(row=1, column=0)
        v_scrollbar.grid(row=1, column=1, sticky='nes')

        self.label = tk.Label(self.master, text="sample2").grid(row=6, columnspan=3)
        
        ccols = ('aa', 'bb')
        self.treeview1 = ttk.Treeview(self.master, columns=ccols)
        v_scrollbar1= ttk.Scrollbar(self.master, orient='vertical', command=self.treeview1.yview)
        self.treeview1.config( yscrollcommand=v_scrollbar1.set)
        for col in ccols:
            self.treeview.heading(col, text=col)
        self.treeview1.grid(row=8, column=0)
        v_scrollbar1.grid(row=8, column=1, sticky='nes')
        
        for i in range(100):
            self.treeview.insert('', 'end', value=(i))

def main():
    root = tk.Tk()
    Window(root)
    root.mainloop()

# --- main ---

if __name__ == '__main__':
    main()

enter image description here

Please let me know if any other information is needed. Thank you

Upvotes: 1

Views: 591

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

The widget for this is called a paned window. Both tkinter and ttk have such widgets.

The PanedWindow works a lot like the ttk Notebook widget. Each pane should be a frame, and you call add to add the frame to the PanedWindow.

Here's a simple example with two Treeview widgets, each with a Scrollbar:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

pw = ttk.PanedWindow(root, orient="vertical")
pw.pack(fill="both", expand=True)

pane1 = ttk.Frame(pw)
pane2 = ttk.Frame(pw)

pw.add(pane1)
pw.add(pane2)

tree1 = ttk.Treeview(pane1)
sb1 = ttk.Scrollbar(pane1, command=tree1.yview)
tree1.configure(yscrollcommand=sb1.set)

sb1.pack(side="right", fill="y")
tree1.pack(side="right", fill="both", expand=True)

tree2 = ttk.Treeview(pane2)
sb2 = ttk.Scrollbar(pane2, command=tree2.yview)
tree2.configure(yscrollcommand=sb2.set)

sb2.pack(side="right", fill="y")
tree2.pack(side="right", fill="both", expand=True)

root.mainloop()

Upvotes: 2

Related Questions