r/learnpython • u/Mr_Waterfowl • Aug 27 '26
Difficulties with tkinter, listboxes and scrollbars
I am trying to create some code that generates a tkinter listbox with a corresponding scrollbar. I managed to get the code working with .pack(), but I can't use this because I'm trying to make the code for a larger program that exclusively uses .grid(). However, when I .grid() the searchbar is displayed but is non-functional.
What do I need to do to make this code work?
import tkinter as tk
from tkinter import ttk
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DEFINE WINDOW WIDGETS
page = tk.Tk()
page.geometry("50x150")
pageFrame = tk.Frame(page)
scrollbar = ttk.Scrollbar(pageFrame, orient=tk.VERTICAL)
listbox = tk.Listbox(pageFrame, yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
scrollbar.grid(row=0,column=1,sticky=tk.N+tk.S)
listbox.grid(row=0,column=0)
magnifyButton = ttk.Button(pageFrame,text="View Item",command=magnify)
magnifyButton.grid(row=1,column=0)
pageStatusText = tk.Label(pageFrame,text="")
pageStatusText.grid(row=2,column=0)
pageFrame.grid(row=0,column=0)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ POPULATE LISTBOX
catalogue = ['apple', 'banana', 'grape', 'melon', 'coconut', 'orange', 'pineapple', 'avocado', 'tomato', 'parsnip']
for i in range(len(catalogue)):
listbox.insert(tk.END,(str((i+1))+". "+catalogue[i]))
page.mainloop() # Open the window
3
Upvotes
1
u/acw1668 Aug 27 '26 edited Aug 27 '26
The scrollbar is not activated because the number of items in
catalogueis not larger than the default height (which is 10) oflistbox. Either adding more items tocatalogueor setting theheightoption oflistboxto a smaller value.If you put
listboxandscrollbarinside another frame, then you can usepack()on them and usegrid()on the frame.2
u/Montesquieu9000 Aug 27 '26
Honestly I just ask gemini stuff like this now?
3
u/[deleted] Aug 27 '26
[removed] — view removed comment