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:

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:

  1. Import Tkinter module.
  2. Create the main window.
  3. Add widgets to the main window.
  4. 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.

Syntax: window=tk.Tk()

To change the dimensions of the created window, Tkinter provides a method geometry.

Syntax: window.geometry(‘200×200’).

3. Add widgets to the main window

Tkinter provides a method ScrollBar to create a scroll bar,.

Syntax: bar=tk.ScrollBar(window)

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’.

Syntax: bar.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.

Syntax: window.mainloop().

Complete code:

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()

Final Output:

double scrollbar frame with Tkinter

Leave a Reply

Your email address will not be published. Required fields are marked *