Python Forum

Full Version: Scrollbar
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using Canvas I get scrollbar in the right side of the form.
Is not possible put the scrollbar inside listbox ?

This is my relevant code

lb = Listbox(root, height=2, width=12, bg='white', fg='black', font=('times new roman', 10), exportselection=0)
canvas.create_window(505, 250, window=lb)
.......
scrollbar = Scrollbar(root)
scrollbar.pack(side="right", fill="y")
lb.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)
There was a set of demo programs written in 2005 (I have the original code, which I partially modified, for python 3.6). This included
a program cscroll.py (and support module template.py)

There's a much improved version for python 3 here: https://stackoverflow.com/a/43788752

If you'd like to see the original 2005 code, let me know, but the above link is a better version.
Thanks for reply

I followed the instructions and now I have the scrollbar at side of listbox.
However, the number of visible rows does not match with which i set for listbox height - i.e. I set height to 3 and get 5 visible rows

This is my relevant code

root = tk.Tk()

canvas = Canvas(root, width = 640, height = 430,  relief = 'raised')
canvas.pack()

frame = Frame(canvas)
canvas.create_window(220, 270, window=frame)

lb = Listbox(frame, height=3, width=10, bg='white', fg='black', font=('times new roman', 9))
lb.insert(END, '1', '2', '3', '4', '5', '6', '7', '8')
lb.pack(side='left', fill='y')

scrollbar = Scrollbar(frame, orient = 'vertical', command=lb.yview)
scrollbar.pack(side="right", fill="y")

lb.config(yscrollcommand=scrollbar.set)
modified your code so that it will run, and it shows the listbox in the frame, three high with the eight entries
unalligned, and not filling root.
from tkinter import * 

root = Tk()
 
canvas = Canvas(root, width = 640, height = 430,  relief = 'raised')
canvas.pack()
 
frame = Frame(canvas)
canvas.create_window(220, 270, window=frame)
 
lb = Listbox(frame, height=3, width=10, bg='white', fg='black', font=('times new roman', 9))
lb.insert(END, '1', '2', '3', '4', '5', '6', '7', '8')
lb.pack(side='left', fill='y')
 
scrollbar = Scrollbar(frame, orient = 'vertical', command=lb.yview)
scrollbar.pack(side="right", fill="y")
 
lb.config(yscrollcommand=scrollbar.set)

root.mainloop()
[attachment=1315]
Thanks again

But, if we set listbox height to 1 or 2, that does not modify the number of visible rows, which remains in 3.
Is this a normal procedure. i.e. there a minimal visible number of rows ?
Where do you have room for a scrollbar if the height is less than 3? If you modify Larz60+ example to set the size to 1 and not expand the list to fill the frame you can see how silly this looks.
from tkinter import *

root = Tk()

canvas = Canvas(root, width = 640, height = 430,  relief = 'raised')
canvas.pack()

frame = Frame(canvas)
canvas.create_window(220, 270, window=frame)

lb = Listbox(frame, height=1, width=10, bg='white', fg='black', font=('times new roman', 9))
lb.insert(END, '1', '2', '3', '4', '5', '6', '7', '8')
lb.pack(side='left')

scrollbar = Scrollbar(frame, orient = 'vertical', command=lb.yview)
scrollbar.pack(side="right", fill="y")

lb.config(yscrollcommand=scrollbar.set)

root.mainloop()
Why would you use a listbox with size = 1 or 2? You can't see any of the list. To get listbox behavior with a small footprint use a combobox.
I would like to save space because I want to use several listboxes - each one containing names of fields for searching a database with several criteria.
Anyway, the problem only occurs in Windows - in MacOs, I get the exact number of visible rows that I have set in listbox height

Thanks again
For a small footprint use combobox. A combobox is like a listbox, but the list is displayed as a popup so it doesn't take up any screen space normally. It also has its own slider (if needed) so you don't have to write code for that.

https://www.pythontutorial.net/tkinter/t...-combobox/