Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Autohiding Scrollbars in Tkinter

Autohiding scrollbars in Tkinter is a useful feature, especially when you want the scrollbars to be visible only when necessary.

Here's a step-by-step tutorial:

Autohiding Scrollbars in Tkinter

  1. Setting up the basic GUI with Tkinter:

    Start by setting up a basic window using Tkinter.

    import tkinter as tk
    
    root = tk.Tk()
    root.title("Autohiding Scrollbars in Tkinter")
    root.geometry("300x200")
    
  2. Creating the AutohideScrollbar class:

    This class extends the default Scrollbar to automatically hide when it's not needed.

    class AutohideScrollbar(tk.Scrollbar):
        def __init__(self, master=None, **kw):
            tk.Scrollbar.__init__(self, master, **kw)
            self._is_needed = False
            self.pack_forget()
    
        def set(self, lo, hi):
            if float(lo) > 0.0 or float(hi) < 1.0:
                if not self._is_needed:
                    self.pack(side=tk.RIGHT, fill=tk.Y)
                    self._is_needed = True
            else:
                if self._is_needed:
                    self.pack_forget()
                    self._is_needed = False
            tk.Scrollbar.set(self, lo, hi)
    
  3. Using the AutohideScrollbar with a Widget (e.g., Text widget):

    Now, integrate the scrollbar with a widget such as the Text widget.

    text_frame = tk.Frame(root)
    text_frame.pack(fill=tk.BOTH, expand=True)
    
    text_widget = tk.Text(text_frame, wrap=tk.NONE)
    text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    
    v_scrollbar = AutohideScrollbar(text_frame, command=text_widget.yview)
    text_widget.config(yscrollcommand=v_scrollbar.set)
    
    h_scrollbar = AutohideScrollbar(text_frame, orient=tk.HORIZONTAL, command=text_widget.xview)
    text_widget.config(xscrollcommand=h_scrollbar.set)
    

    Notice that two scrollbars (v_scrollbar and h_scrollbar) are used for vertical and horizontal scroll, respectively.

  4. Run the main loop:

    root.mainloop()
    

When you combine all the code snippets above and run the script, you'll get a Tkinter window with a Text widget. At first, no scrollbars will be visible. As you add more text and exceed the widget's viewable area, the relevant scrollbar (either vertical or horizontal) will appear. When the content fits within the widget's boundaries again, the scrollbar will auto-hide.

  1. How to hide scrollbars in Tkinter when not in use:

    • Description: Learn how to make the scrollbars in Tkinter widgets disappear when they are not actively being used by the user.
    • Code:
      from tkinter import Tk, Scrollbar, Text, RIGHT, Y
      
      def on_mousewheel(event):
          text.yview_scroll(int(-1 * (event.delta / 120)), "units")
      
      root = Tk()
      text = Text(root, wrap="none", yscrollcommand=lambda *args: scrollbar.set(*args))
      scrollbar = Scrollbar(root, command=text.yview)
      scrollbar.pack(side=RIGHT, fill=Y)
      text.pack(expand=True, fill="both")
      
      text.bind("<MouseWheel>", on_mousewheel)
      
      root.mainloop()