A scrollbar widget helps users see all parts of another widget, whose content is typically much larger than what can be shown in the available screen space. Scrollbars are used with Text, Listbox, ...
Scrollbars are not a part of another widget but are a separate widget altogether. Instead, scrollbars communicate with the 'scrolled widget' by calling methods on the scrolled widget; the scrolled widget also needs to call methods on the scrollbar.
Every widget that can be scrolled vertically includes a method named 'yview', while those that can be scrolled horizontally have a method named 'xview'.
The scrolled widget also needs to communicate back to the scrollbar, telling it what percentage of the entire content area is now visible. Every scrollable widget also has a 'yscrollcommand' and/or 'xscrollcommand' configuration option. This is used to specify a method call, which must be the scrollbar's 'set' method.
# +---------+-----------+ # | Listbox | Scrollbar | # +---------+-----------+ # | Button | # +---------------------+ # scrollbar1.py import tkinter as tk root = tk.Tk() # root widget root.rowconfigure(0, weight=1) # row 1 will not expand (button) root.columnconfigure(0, weight=1) # column 1 will not expand (scrollbar) listbox = tk.Listbox(root, selectmode=tk.BROWSE) listbox.grid(row=0, column=0, sticky=tk.NSEW) for item in ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]: listbox.insert(tk.END, item) yscroll = tk.Scrollbar(root, orient=tk.VERTICAL) yscroll.grid(row=0, column=1, sticky=tk.NS) yscroll.config(command=listbox.yview) listbox.config(yscrollcommand=yscroll.set) button = tk.Button(root, text="Choices", command=lambda: print ([listbox.get(i) for i in listbox.curselection()])) button.grid(row=1, column=0, columnspan=2) root.mainloop()
Selected keyword arguments in Scrollbar() master=root # the first argument orient=tk.VERTICAL # default orient=tk.HORIZONTAL command=listbox.yview # for Listbox widget command=textbox.yview # for Text widget
# +---------+---------+ # | textbox | yscroll | # +---------+---------+ # | xscroll | (empty) | # +---------+---------+ # textbox3.py import tkinter as tk root = tk.Tk() # empty window root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) textbox = tk.Text(root, height=4, width=20, wrap=tk.NONE) textbox.grid(row=0, column=0, sticky=tk.NSEW) yscroll = tk.Scrollbar(root, orient=tk.VERTICAL) yscroll.grid(row=0, column=1, sticky=tk.NS) xscroll = tk.Scrollbar(root, orient=tk.HORIZONTAL) xscroll.grid(row=1, column=0, sticky=tk.EW) yscroll.config(command=textbox.yview) textbox.config(yscrollcommand=yscroll.set) xscroll.config(command=textbox.xview) textbox.config(xscrollcommand=xscroll.set) quote = """HAMLET: To be, or not to be--that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them. To die, to sleep-- No more--and by a sleep to say we end The heartache, and the thousand natural shocks That flesh is heir to. 'Tis a consummation Devoutly to be wished.""" textbox.insert(tk.END, quote) root.mainloop() # run the tkinter event loop