As with menu Tkinter widgets, we will need some explanations with Labels what specific widgets are used for:
lab_1 = Label(top_win, text ="Scrolls are Weird")
lab_1.pack(side = TOP, anchor = NW)
Scrollbars must be positioned inside some Tk window:
scr_bar = Scrollbar(top_win)
scr_bar.pack(side = RIGHT, fill = Y)
Important - now we need Listbox with yscrollcommand that will target Scrollbar, in this case scr_bar:
items = Listbox(top_win, yscrollcommand = scr_bar.set)
If we are lazy to invent items, there is a for loop to populate a list:
for x in range(100):
items.insert(END, "Item: " + str(x))
Now it's time to pack Listbox with items:
items.pack(side = LEFT, fill = BOTH)
Interesting thing with Scrollbars, they must be configured to target items, using this line:
scr_bar.config(command = items.yview)
This is full working source:
from tkinter import *
import tkinter.messagebox as mb
import os
top_win = Tk()
top_win.geometry("200x200")
lab_1 = Label(top_win, text ="Scrolls are Weird")
lab_1.pack(side = TOP, anchor = NW)
scr_bar = Scrollbar(top_win)
scr_bar.pack(side = RIGHT, fill = Y)
items = Listbox(top_win, yscrollcommand = scr_bar.set)
for x in range(100):
items.insert(END, "Item: " + str(x))
items.pack(side = LEFT, fill = BOTH)
scr_bar.config(command = items.yview)
top_win.mainloop()
Scrollbars are little bit weird, so definitely check YouTube tutorial on Scrollbars.
No comments:
Post a Comment