Daniel Janda
Daniel Janda

Reputation: 73

How to pack a horizontal scrollbar under the listbox (python and tkinter)?

I am writing a program that requires output to be put into a list box, and it sometimes exceeds the x size of the listbox. I added a horizontal scrollbar and it scrolls horizontally with no problem but doesn't get packed under the listbox and instead gets packed next to it.

Is there a way to solve this ? I probably could solve it using .grid(), but I need to keep it using .pack() due to some other problems.

from tkinter import *

master = Tk()

scrollbary = Scrollbar(master)
scrollbarx = Scrollbar(master, orient=HORIZONTAL)

listbox = Listbox(master, yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
for i in range(1, 100):
    listbox.insert(END, "NUMBEEEEEEEEEEEEEEEEEEEEEEEEEEEEER:")
    listbox.insert(END, str(i))

scrollbary.config(command=listbox.yview)
scrollbarx.config(command=listbox.xview)

listbox.pack(side=LEFT, fill=BOTH)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.pack(side=BOTTOM, fill=X)

mainloop()

I've tried some googling but didn't find anything useful, or it was not understandable with my kind of limited knowledge of python and tkinter... Thanks for any help!

Upvotes: 2

Views: 4015

Answers (1)

Daniel Janda
Daniel Janda

Reputation: 73

The problem was the packing order. I tried reordering it to:

  1. scrollbar_x
  2. listbox
  3. scrollbar_y

and now it works. Thanks for your time @stovfl !

Upvotes: 3

Related Questions