I am trying to insert a vertical scrollbar in a treeview such that it is displayed under the columns labels of the treeview and not besides/next to the labels. I’ve tried adding pady in the scrollbar widget yet that still does not place it under the columns labels (just creates an offset from the top). Any help is greatly appreciated (looking at your @Bryan Oakley). I’ve tried numerous padding techniques to make the vertical scrollbar start below the columns labels yet nothing has worked thus far. Here is a minimal working code:
from tkinter import * from tkinter import ttk root = Tk() root.geometry('400x150') treeFrame = Frame(root) treeFrame.grid(row=0, column=0, sticky='nsew') root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) treeFrame.rowconfigure(0, weight=1) treeFrame.columnconfigure(0, weight=1) tree=ttk.Treeview(treeFrame, selectmode='browse') tree.grid(row=0, column=0, sticky='nsew') tree['columns']='LastName' tree.column('#0', width=50) tree.column('LastName', width=50) tree.heading('#0', text='FirstName', anchor='w') tree.heading('LastName', text='LastName', anchor='w') tree.insert(parent='', index='end', iid=0, text='Jack', values=('Sparrow')) tree.insert(parent='', index='end', iid=1, text='Harry', values=('Potter')) tree.insert(parent='', index='end', iid=3, text='Jack1', values=('Sparrow')) tree.insert(parent='', index='end', iid=4, text='Harry1', values=('Potter')) tree.insert(parent='', index='end', iid=5, text='Jack2', values=('Sparrow')) tree.insert(parent='', index='end', iid=6, text='Harry2', values=('Potter')) tree.insert(parent='', index='end', iid=7, text='Jack3', values=('Sparrow')) tree.insert(parent='', index='end', iid=8, text='Harry3', values=('Potter')) scrollY = Scrollbar(treeFrame, orient='vertical', command=tree.yview) scrollY.grid(row=0, column=1, sticky='ns') tree['yscrollcommand'] = scrollY.set s= ttk.Style() s.theme_use('clam') root.mainloop()
Advertisement
Answer
You can put the scrollbar at the right side of the cell of tree
:
... s = ttk.Style() s.theme_use('clam') tree.update() # make sure tree is updated bbox = tree.bbox(0) # get bounding box of first row (iid=0) # bbox[1] will be the top y of first row scrollY.grid(row=0, column=0, sticky='nse', pady=(bbox[1],2), padx=(0,2)) root.mainloop()
Note that it only works when there is at least one row inserted.