Python Forum
[Tkinter] Scrollbar
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Scrollbar
#1
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)
Yoriz write Oct-10-2021, 08:18 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply
#3
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)
Reply
#4
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()
   
Reply
#5
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 ?
Reply
#6
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.
Reply
#7
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
Reply
#8
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/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help create scrollbar in chatbot with tkinter on python fenec10 4 1,516 Aug-07-2023, 02:59 PM
Last Post: deanhystad
Question [Tkinter] How to configure scrollbar dimension? water 6 3,435 Jan-03-2021, 06:16 PM
Last Post: deanhystad
  [PyQt] scrollbar in tab issac_n 1 3,600 Aug-04-2020, 01:33 PM
Last Post: deanhystad
  [Tkinter] Scrollbar in tkinter PatrickNoir 2 3,304 Jul-26-2020, 06:02 PM
Last Post: deanhystad
  [Tkinter] Help with Scrollbar JJota 6 3,653 Mar-10-2020, 05:25 AM
Last Post: Larz60+
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 9,358 Sep-19-2019, 03:26 PM
Last Post: joe_momma
  [Tkinter] Same Scrollbar for two text area smabubakkar 3 2,854 Jun-19-2019, 05:26 PM
Last Post: Denni
  Scrollbar rturus 5 16,953 Jun-06-2019, 01:04 PM
Last Post: heiner55
  [PyGUI] Create a scrollbar in GUI to add test cases mamta_parida 1 3,618 Sep-27-2018, 11:57 AM
Last Post: Larz60+
  [Tkinter] Scrollbar problem & general organization weatherman 13 13,430 Apr-16-2017, 12:55 PM
Last Post: weatherman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020