Python 3 – Tkinter Scrollbar
Graphical user interfaces (GUIs) provide a more intuitive way for users to interact with applications. Tkinter is a module in Python that allows developers to create GUI applications. A scrollbar is a widget that can be added to any Tkinter container widget like a Frame, Canvas, or Listbox, to allow users to scroll through the content of the container. In this article, we will explore how to add a scrollbar to a Tkinter window or frame.
Adding a Scrollbar to a Window
Let’s start by creating a basic window with a Text widget, and then adding a scrollbar to it.
import tkinter as tk
# create a window
root = tk.Tk()
root.geometry("400x400")
# add a Text widget to the window
text_widget = tk.Text(root)
text_widget.pack(fill="both", expand=True)
# add a scrollbar to the right of the Text widget
scrollbar = tk.Scrollbar(root, command=text_widget.yview)
scrollbar.pack(side="right", fill="y")
# configure the Text widget to use the scrollbar
text_widget.configure(yscrollcommand=scrollbar.set)
# add some text to the Text widget
text_widget.insert("end", "Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
# run the mainloop
root.mainloop()
In this example, we created a Text widget and added it to the window using the pack() method. We then created a scrollbar widget and added it to the right side of the window using the side parameter of the pack() method. We set the fill parameter to “y” so that the scrollbar stretches vertically to fill the available space. We also passed the command parameter to the scrollbar widget to set the callback function to be called when the scrollbar is moved. In this case, we passed the yview method of the Text widget to the scrollbar so that it can control the scrolling of the widget. Finally, we configured the Text widget to use the scrollbar for vertical scrolling by setting the yscrollcommand parameter to scrollbar.set().
Adding a Scrollbar to a Frame
Adding a scrollbar to a frame is similar to adding one to a window. Let’s create a frame with a canvas widget and add a scrollbar to it.
import tkinter as tk
# create a window
root = tk.Tk()
root.geometry("400x400")
# create a frame
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)
# add a canvas to the frame
canvas = tk.Canvas(frame)
canvas.pack(side="left", fill="both", expand=True)
# add a scrollbar to the right of the canvas
scrollbar = tk.Scrollbar(frame, command=canvas.yview)
scrollbar.pack(side="right", fill="y")
# configure the canvas to use the scrollbar
canvas.configure(yscrollcommand=scrollbar.set)
# create a new frame inside the canvas
inner_frame = tk.Frame(canvas)
# add some widgets to the inner frame
for i in range(50):
label = tk.Label(inner_frame, text=f"Label {i+1}")
label.pack()
# add the inner frame to the canvas
canvas.create_window((0, 0), window=inner_frame)
# update the scrollbar when the canvas size changes
def update_scroll_region(event):
canvas.configure(scrollregion=canvas.bbox("all"))
canvas.bind("<Configure>", update_scroll_region)
# run the mainloop
root.mainloop()
In this example, we created a frame and a canvas widget, and added the canvas to the left side of the frame using the pack() method. We then created a scrollbar widget and added it to the right side of the frame. We passed the command parameter to the scrollbar widget to set the callback function to be called when the scrollbar is moved. In this case, we passed the yview method of the canvas widget to the scrollbar so that it can control the scrolling of the widget. We configured the canvas widget to use the scrollbar for vertical scrolling by setting the yscrollcommand parameter to scrollbar.set().
Next, we created an inner frame inside the canvas and added some widgets to it. We used the create_window() method of the canvas to add the inner frame as a child of the canvas. We also bound the update_scroll_region() function to the
Conclusion
In this article, we have explored how to add a scrollbar to a Tkinter window or frame. We have also seen how to use this scrollbar with Text, Canvas, and Listbox widgets. With this knowledge, you can create more advanced GUI applications that require scrolling capabilities.