Home > Software design >  Scrollbar for TreeView - Python
Scrollbar for TreeView - Python

Time:10-29

I have been trying to attach a vertical scrollbar for the tkinter treeview in Python. For some reason it shows up under TreeView not on the right side.

screenshot

TreeView is in the frame

fFetchActivity = LabelFrame(root, text="Spis Twoich Aktywności", padx=50, pady=15)
fFetchActivity.place(x=20, y=200)

and TreeView and Scrollbar code:

tv1 = ttk.Treeview(fFetchActivity)
tv1.pack()
scrollbar_object = Scrollbar(fFetchActivity, orient="vertical")
scrollbar_object.pack(side=RIGHT, fill=Y)
scrollbar_object.config(command=tv1.yview)
tv1.configure(yscrollcommand=scrollbar_object.set)

Does anyone have any ideas on how to improve this? Thanks!

CodePudding user response:

The default for pack is to place the widget along the top of the unallocated space. So, when you do tv1.pack(), tv1 is placed at the top of the window. Another aspect of pack is that when a widget is placed along an edge, it is allocated the entire edge. For example, once a widget is placed along the top, it is not possible to put something to the right or left side because the widget is allocated the entire top edge.

If you want a widget to be to the right of another widget, you should call pack on that item first so that it is allocated the entire right side. You can then pack the other widget however you want in the remaining space.

In your case, this is typically how you would do it with pack to get the treeview to take up as much space as possible, and for the scrollbar to be on the right.

scrollbar_object.pack(side="right", fill="y")
tv1.pack(side='left', fill="both", expand=True)
  • Related