16. tk Scrollbar
16.1. Usage
The
tkinter.Scrollbar widget provides a horizontal or vertical scrollbar that allows the user to scroll the contents of another widget.A scrollbar is commonly used with widgets such as
Text, Canvas, Listbox and Treeview.To create a scrollbar widget, the general syntax is
(assuming import via
import tkinter as tk):- scrollbar_widget = tk.Scrollbar(parent, option=value)
parentis the window or frame object.Options can be passed as parameters separated by commas.A scrollbar must be linked to another widget using the widget'sxvieworyviewmethod.
16.2. Using a Scrollbar
import tkinter as tk
root = tk.Tk()
root.title("Scrollbar Example")
scrollbar = tk.Scrollbar(root)
scrollbar.pack(side="right", fill="y")
text = tk.Text(root, width=40, height=10,
yscrollcommand=scrollbar.set)
text.pack(side="left", fill="both", expand=True)
scrollbar.config(command=text.yview)
for i in range(1, 21):
text.insert("end", f"Line {i}\n")
root.mainloop()
Tasks
Modify the program so that:
Modify the code to produce the layout shown above.
import tkinter as tk
root = tk.Tk()
root.title("Scrollbar Question")
scrollbar = tk.Scrollbar(root)
scrollbar.pack(side="right", fill="y")
text = tk.Text(
root,
width=40,
height=10,
yscrollcommand=scrollbar.set
)
text.pack(side="left")
scrollbar.config(command=text.yview)
for i in range(1, 101):
text.insert("end", f"Line {i}\n")
root.mainloop()
16.3. Methods
- scrollbar_widget.activate(element=None)
- Activates the specified scrollbar element.If no element is supplied, returns the currently active element.
- scrollbar_widget.delta(deltax, deltay)
- Returns the amount the scrollbar should move for the specified mouse movement.
- scrollbar_widget.fraction(x, y)
- Returns the fractional position corresponding to the specified coordinates.
- scrollbar_widget.get()
- Returns the current position of the scrollbar as two fractions.
- scrollbar_widget.identify(x, y)
- Returns the name of the scrollbar element at the specified coordinates.
- scrollbar_widget.set(first, last)
- Updates the slider position.This method is normally called automatically by the associated widget.
16.4. Parameter syntax
- scrollbar_widget = tk.Scrollbar(parent, option=value)
parentis the window or frame object.Options can be passed as parameters separated by commas.Parameters:
- activebackground
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, activebackground="color")Description: Sets the colour of the slider while it is active.Default: SystemButtonFace
- background
- bg
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, bg="color")Description: Sets the background colour of the scrollbar.Default: SystemButtonFace
- borderwidth
- bd
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, borderwidth=value)Description: Sets the border width.Default: 2
- command
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, command=function)Description: Specifies the function used to scroll the associated widget.Example:command=text.yview
- cursor
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, cursor="cursor_type")Description: Sets the mouse cursor.
- elementborderwidth
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, elementborderwidth=value)Description: Sets the border width of the slider and arrow buttons.
- jump
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, jump=True)Description: Updates the associated widget only after the slider is released.Default: False
- orient
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, orient="vertical")Description: Specifies the scrollbar orientation.Valid values:"vertical","horizontal"Default:"vertical"
- relief
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, relief="style")Description: Sets the border style.Default:sunken
- repeatdelay
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, repeatdelay=milliseconds)Description: Specifies how long to wait before auto-repeating begins.Default: 300
- repeatinterval
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, repeatinterval=milliseconds)Description: Specifies the interval between repeated scrolling actions.Default: 100
- takefocus
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, takefocus=1)Description: Determines whether the scrollbar can receive keyboard focus.
- troughcolor
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, troughcolor="color")Description: Sets the colour of the scrollbar trough.
- width
- Syntax:
scrollbar_widget = tk.Scrollbar(parent, width=value)Description: Sets the width of a vertical scrollbar or the height of a horizontal scrollbar.Default: Platform dependent
16.5. Default options
Code to display the default value for each
Scrollbar option is shown below.import tkinter as tk
root = tk.Tk()
widget = tk.Scrollbar(root)
widget_options = widget.keys()
for option in widget_options:
print(f"{option}: {widget.cget(option)}") # cget retrieves the current value of the option