I'm learning how to do GUI in python, I am trying to make a scrollbar for my listbox but it's not showing up. I tried many different things with the same results, so to make sure that I am not doing something wrong I went to the example in
The code for the example is:
from tkinter import *
root = Tk()
root.geometry("150x200")
w = Label(root, text ='GeeksForGeeks',
font = "50")
w.pack()
scroll_bar = Scrollbar(root)
scroll_bar.pack( side = RIGHT,
fill = Y )
mylist = Listbox(root,
yscrollcommand = scroll_bar.set )
for line in range(1, 26):
mylist.insert(END, "Geeks " str(line))
mylist.pack( side = LEFT, fill = BOTH )
scroll_bar.config( command = mylist.yview )
root.mainloop()
CodePudding user response:
Because of the size of the window, the listbox is too small to have a scrollbar. Decrease the size of the window by adding root.geometry("50x50")
before the root.mainloop()
.
CodePudding user response:
I use to face a similar problem. It can be fixed by defining the scrollbar and packing it before packing the listbox.
Here is the final code:
from tkinter import *
root = Tk()
root.geometry("150x200")
w = Label(root, text ='GeeksForGeeks',
font = "50")
w.pack()
mylist = Listbox(root)
scroll_bar = Scrollbar(root, command=mylist.yview, orient=VERTICAL)
scroll_bar.pack( side = RIGHT,
fill = Y )
for line in range(1, 26):
mylist.insert(END, "Geeks " str(line))
mylist.pack( side = LEFT, fill = BOTH )
mylist.config( yscrollcommand = scroll_bar.set )
root.mainloop()
Thank You!