Horizontal scrollbar in Tkinter – Python
In this tutorial, we will learn how to add a horizontal scrollbar in your Python applications built with Tkinter.
Add a horizontal scrollbar in Tkinter
Python language provides a lot of frameworks to enhance the Graphical User Interface. Among many, Tkinter is the simplest and easiest way to build your frontend part. Since both languages are in Python at the front end and backend, integration is simple and easy, too.
import tkinter as tk root = tk.Tk() root.title("Horizontal Scrollbar Example") text_widget = tk.Text(root, wrap="none") text_widget.pack(side="top", fill="both", expand=True) long_text = "Today is 23 January 2024 " * 100 text_widget.insert("1.0", long_text) h_scroll = tk.Scrollbar(root, orient="horizontal", command=text_widget.xview) h_scroll.pack(side="bottom", fill="x") text_widget.config(xscrollcommand=h_scroll.set) root.mainloop()
Code explanation
First, we import the module and then use tk.Tk()
, we will create a window, and all the work and renders will be here. Now, add a text on the interface using the text_widget
feature. Remember to keep the parameter wrap = "none"
for the scrolling feature.
Now, you have to add a scrolling feature. You can use the tk.Scrollbar()
function and keep the orient="horizontal"
for horizontal scrolling. You have to use .pack()
function after inserting any widget so that Tkinter can organize it.
At last, run the GUI using root.mainloop()
.
Output
After running the Python file, you can see the following output window.
Leave a Reply