user1956185
user1956185

Reputation: 389

Scrollbars Tkinter

I have two listboxes one next to the other and I want each of them to have horizontal and vertical scrollbars. I have managed to create them, but when I press the arraws on the scrollbars, nothing happens, and I cannot see the entire content.

This is the code I have so far:

from Tkinter import *

root = Tk()

frame4 = Frame(root)
frame4.grid(row=2,columnspan=2,sticky=E+W)
l5 = Label(frame4, text='Output:').grid(row=0,columnspan=2)

frame5 = Frame(frame4)
frame5.grid(row=1,column=0,sticky=E+W)
l6 = Label(frame5, text='Algo1:').pack()
yscroll1 = Scrollbar(frame5, orient=VERTICAL)
xscroll1 = Scrollbar(frame5, orient=HORIZONTAL)

output_algo = Listbox(frame5,height=5, width=40)
output_algo.config (yscrollcommand=xscroll1.set)
output_algo.config (yscrollcommand=yscroll1.set)
yscroll1.config (command=output_algo.yview)
xscroll1.config (command=output_algo.yview)

yscroll1.pack(side=RIGHT, fill=Y,expand=0)
xscroll1.pack(side=BOTTOM, fill=X,expand=0)

output_algo.pack(side=LEFT,fill=BOTH,expand=1)


frame6 = Frame(frame4)
frame6.grid(row=1,column=1,sticky=E+W)
l7 = Label(frame6, text='Algo2:').pack()
yscroll2 = Scrollbar(frame6, orient=VERTICAL)
xscroll2 = Scrollbar(frame6, orient=HORIZONTAL)

output_opt = Listbox(frame6,height=5, width=40)
output_opt.config (yscrollcommand=xscroll2.set)
output_opt.config (yscrollcommand=yscroll2.set)
yscroll2.config (command=output_opt.yview)
xscroll2.config (command=output_opt.yview)

yscroll2.pack(side=RIGHT, fill=Y,expand=0)
xscroll2.pack(side=BOTTOM, fill=X,expand=0)

output_opt.pack(side=LEFT,fill=BOTH,expand=1)

root.mainloop()

How can I change it for the scrollbars to work?

Also, I read that using grid in the case of scrollbars is better than pack. If I were to modify my code to use grid would I have to create a frame which contains only the listbox and the scrollbar?

Upvotes: 0

Views: 182

Answers (2)

furas
furas

Reputation: 142631

You can't scroll empty Listbox.

I added some elements to listbox and I could scroll up and down.

for item in range(30):
    output_algo.insert(END, str(item)*item)
    output_opt.insert(END, str(item)*item)

root.mainloop()

There is some problem with scrolling left to right but I saw mistake in xscroll1.config()

Upvotes: 0

MrAlias
MrAlias

Reputation: 1346

Likely copy and paste was not your friend:

output_algo.config (yscrollcommand=xscroll1.set)

should be

output_algo.config (xscrollcommand=xscroll1.set)

and

xscroll1.config (command=output_algo.yview)

should be

xscroll1.config (command=output_algo.xview)

And of course you'll need to do the same for the other Listbox :)

Upvotes: 3

Related Questions