How to make a double scrollbar frame in Tkinter in Python
Here we will learn how to make both horizontal and vertical scrollbar in Tkinter Python.
The basics required for creating a double scrollbar frame with Tkinter are:
Tkinter is a Python module used to create GUI applications. John Ousterhout developed this module in the year 1991. It helps in creating GUI applications with the least effort efficiently.
Steps involved in creating a double scrollbar with Tkinter:
- Import Tkinter module.
- Create the main window.
- Add widgets to the main window.
- Execute the application.
1. Import Tkinter module
Importing Tkinter is very easy, you can import it as follows.
For Python 3 or above.
Syntax: import tkinter as tk
For users working with Python 2.
Syntax: import Tkinter as tk
Note: ‘as’ indicates that from here on Tkinter will be referred to as tk.
2. Create the main window
Tkinter offers a method of Tk to create a window.
To change the dimensions of the created window, Tkinter provides a method geometry.
3. Add widgets to the main window
Tkinter provides a method ScrollBar to create a scroll bar,.
ScrollBar method provides attributes like orient which determines the orientation of the scrollbar. The default orientation is vertical.
To organize the widgets in blocks before placing in the parent widget Tkinter provides a method ‘pack’.
To organize the scrollbar pack provides some attributes like side.
The side attribute determines where the scroll bar must be located (i.e. right, left, top, or bottom).
4. Execute the Application
When you are through with the steps above run the mainloop() function. Mainloop creates an infinite loop that waits for an event to occur and processes the event until the window is closed.
import tkinter as tk window = tk.Tk() window.geometry("200x200") bar = tk.Scrollbar(window) bar.pack (side = tk.RIGHT, fill = "y") bar = tk.Scrollbar(window, orient = tk.HORIZONTAL) bar.pack (side = tk.BOTTOM, fill = "x") window.mainloop()