tkinterscrollbarpython-3.9

Set scrollbar to the LEFT with grid layout manager


I get read, that you can place the scollbar to the LEFT in .pack() layout manager, but I can't find a working solution for .grid() layout manager. The description in my book say I should use sticky = 'nsw' for location. Than I tried to move it to the left, see my example.

This doesn't have any effect:

import tkinter as tk
from tkinter import ttk
from pathlib import Path

root = tk.Tk()

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

paths = Path('.').glob('**/*')

tv = ttk.Treeview(root, columns=['size','modified'], selectmode=None)

tv.heading('#0', text='Name')
tv.heading('size', text='Size', anchor='center')
tv.heading('modified', text='Modifies', anchor='center')

tv.column('#0', stretch = True, anchor='w')
tv.column('size', width=100, anchor='center') 
tv.column('modified',anchor='center')

tv.grid_rowconfigure(0, weight=1)
tv.grid_columnconfigure(0, weight=1) 
tv.grid(row=0, column=0, sticky='nsew')
#tv.pack(expand=True, fill='both')

for path in paths:
    meta = path.stat()
    parent = str(path.parent)
    if parent == '.':
        parent = ''
               
    tv.insert(parent, 'end', iid=str(path), text=str(path.name), values=[meta.st_size, meta.st_mtime])

scrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL, command=tv.yview)
tv.configure(yscrollcommand=scrollbar.set)
scrollbar.grid(row=0, column=1, sticky='nse') #scrollbar goes not to the left!
        
root.mainloop()

Is it in treeview not possible to locate the the scrollbar to the LEFT side of the window?


Solution

  • If you change tv's column to 1 and scrollbar's to 0, it'll do what you want

    import tkinter as tk
    from tkinter import ttk
    from pathlib import Path
    
    root = tk.Tk()
    
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    
    paths = Path('.').glob('**/*')
    
    tv = ttk.Treeview(root, columns=['size','modified'], selectmode=None)
    
    tv.heading('#0', text='Name')
    tv.heading('size', text='Size', anchor='center')
    tv.heading('modified', text='Modifies', anchor='center')
    
    tv.column('#0', stretch = True, anchor='w')
    tv.column('size', width=100, anchor='center')
    tv.column('modified',anchor='center')
    
    tv.grid_rowconfigure(0, weight=1)
    tv.grid_columnconfigure(0, weight=1)
    tv.grid(row=0, column=1, sticky='nsew')  # set this to column 1
    #tv.pack(expand=True, fill='both')
    
    for path in paths:
        meta = path.stat()
        parent = str(path.parent)
        if parent == '.':
            parent = ''
    
        tv.insert(parent, 'end', iid=str(path), text=str(path.name), values=[meta.st_size, meta.st_mtime])
    
    scrollbar = ttk.Scrollbar(root, orient=tk.VERTICAL, command=tv.yview)
    tv.configure(yscrollcommand=scrollbar.set)
    scrollbar.grid(row=0, column=0, sticky='ns')  # set this to column 0 
    # (you can also just use sticky='ns' if you want)
    
    root.mainloop()