2

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!

Daniel Janda
  • 73
  • 1
  • 9
  • Read [Adding a scrollbar](https://stackoverflow.com/a/3092341/7414759) – stovfl Jan 27 '19 at 10:49
  • That doesn't include information about placing horizontal scrollbars though... – Daniel Janda Jan 27 '19 at 11:50
  • *"placing horizontal scrollbars"*: Change to `orient=HORIZONTAL` and `.pack(side="bottom"...`. The **core** of the linked example are to use **all** `Frame(...`, `Canvas` as shown. `self.frame = tk.Frame` becomes your `listbox = Listbox(...`. – stovfl Jan 27 '19 at 11:57
  • Just reordered the order in which the stuff was packed and it works. Thanks for your time! – Daniel Janda Jan 27 '19 at 12:01

1 Answers1

3

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 !

Daniel Janda
  • 73
  • 1
  • 9