0

I am trying to add horizontal and vertical scrollbars to my Treeview table. this is the part of my code related to this problem. My question is why it does not bring the horizontal scrollbar below the Treeview table?

from tkinter import ttk
from tkinter import *

new_window = Tk()
new_window.geometry("400x400")
new_window.resizable(False, False)

frame1 = LabelFrame(new_window)
frame1.pack(fill="both", expand=False)

tree = ttk.Treeview(frame1, height=3)
tree.pack(side="left")

# Vertical Scrollbar
vsb = ttk.Scrollbar(frame1, orient="vertical", command=tree.yview)
vsb.pack(side="right", fill="y")

# Horizontal Scrollbar
hsb = ttk.Scrollbar(frame1, orient="horizontal", command=tree.xview)
hsb.pack(side="bottom", fill="x")

tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

new_window.mainloop()
  • 1
    Order of packing the widgets matters. Pack the bottom scrollbar first, then right scrollbar and left treeview. Suggest to use `grid()` instead of `pack()`. – acw1668 Apr 19 '22 at 06:09
  • @acw1668 but my Treeview is the first one, and should be on the left side. –  Apr 19 '22 at 06:43
  • The treeview is still packed on the left side when packing those widgets in the suggested order. – acw1668 Apr 19 '22 at 07:28
  • Does [this](https://stackoverflow.com/a/57396569/7432) answer your question? It's a slightly different problem, but it contains several images that describe how the packer works. – Bryan Oakley Apr 19 '22 at 14:27
  • Yes, but I cannot use scrollbar first because it use the treeview name in that. The first one should be Treeview on the left side, then the scrollbars. in this way the order would not be like the ones you said. –  Apr 19 '22 at 14:28
  • In this particular case you don't have to create them in a particular order. Group all of your calls to `pack` after creating all of the widgets. – Bryan Oakley Apr 19 '22 at 14:29

1 Answers1

0

My question is why it does not bring the horizontal scrollbar below the Treeview table?

To fix problem:

  • Btw, I just added ttk for every widgets
  • Move a frame1.pack and tree.pack before maniloop().
  • Add fill='both', expand=True in Treeview.pack() in case you wanted to expand

Snippet:

from tkinter import ttk
from tkinter import *


new_window = Tk()
new_window.geometry("400x400")
new_window.resizable(False, False)

frame1 = LabelFrame(new_window)
frame1.pack(fill="both", expand=False)

tree = ttk.Treeview(frame1, height=3)
 
vsb = ttk.Scrollbar(frame1, orient="vertical", command=tree.yview)
vsb.pack(side="right", fill="y")

hsb = ttk.Scrollbar(frame1, orient="horizontal", command=tree.xview)
hsb.pack(side="bottom", fill="x")

tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

frame1.pack(fill="both", expand=False)
tree.pack(side="left", fill='both', expand=True)

new_window.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19