Open In App
Related Articles

Autohiding Scrollbars using Python-tkinter

Improve Article
Save Article
Like Article

Before moving on to the topic lets see what is Python Tkinter. So, we all know that Python has different options for creating GUI(s) and tkinter is one of them. It is the standard GUI library for Python. And it makes the creation of GUI applications very quick still simple when python is merged with it. It also gives a very effective object-oriented interface to the Tk GUI toolkit.

Note: For more information, refer to Python GUI – tkinter

Widgets in tkinter

Moreover, Tkinter enables number of controls like labels, text boxes, list boxes, buttons, scrollbars etc, which are used in a GUI applications. These controls are known as widgets.

Geometry management methods of tkinter

These methods are used to organize widgets across parents widget area. Moreover, these methods can be accessed by all the tkinter widgets. There are three geometry management methods namely pack(), grid(), and place(). All these methods have different roles.
Now, lets discuss about the topic Autohiding Scrollbars using Python-tkinter.
In this topic, we will see how auto-hiding scrollbars are created using tkinter in Python. So, firstly lets see the meaning of auto hiding scrollbars below:

Auto-hiding Scrollbars

When a scrollbar hides itself if its not required i.e, it is not visible when its not needed then that type of scrollbar is known as Auto-hiding Scrollbar. In Python Autohiding scrollbars can be used with Listbox and Text widgets. It can be implemented using python tkinter with the help of some geometry management methods.
Below examples illustrate the use of Autohiding Scrollbars using Python-tkinter:
Example 1:

# Python program to illustrate the usage of 
# autohiding scrollbars using tkinter
# Importing tkinter
from tkinter import *
# Creating class AutoScrollbar
class AutoScrollbar(Scrollbar):
    # Defining set method with all 
    # its parameter
    def set(self, low, high):
        if float(low) <= 0.0 and float(high) >= 1.0:
            # Using grid_remove
  "grid", "remove", self)
        Scrollbar.set(self, low, high)
    # Defining pack method
    def pack(self, **kw):
        # If pack is used it throws an error
        raise (TclError,"pack cannot be used with \
        this widget")
    # Defining place method
    def place(self, **kw):
        # If place is used it throws an error
        raise (TclError, "place cannot be used  with \
        this widget")
# creating tkinter window 
root = Tk()
# Defining vertical scrollbar
verscrollbar = AutoScrollbar(root)
# Calling grid method with all its
# parameter w.r.t vertical scrollbar
verscrollbar.grid(row=0, column=1
# Defining horizontal scrollbar
horiscrollbar = AutoScrollbar(root, 
# Calling grid method with all its 
# parameter w.r.t horizontal scrollbar
horiscrollbar.grid(row=1, column=0
# Creating scrolled canvas
canvas = Canvas(root,
canvas.grid(row=0, column=0, sticky=N+S+E+W)
# Making the canvas expandable
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
# creating canvas contents
frame = Frame(canvas)
frame.rowconfigure(1, weight=1)
frame.columnconfigure(1, weight=1)
# Defining number of rows and columns
rows = 20
for i in range(1,rows):
    for j in range(1,9):
        button = Button(frame, padx=8, pady=8
                        text="[%d,%d]" % (i,j))
        button.grid(row=i, column=j, sticky='news')
# Creating canvas window
canvas.create_window(0, 0, anchor=NW, window=frame)
# Calling update_idletasks method
# Configuring canvas
# Calling mainloop method


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Mar, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials