Open In App

How to make a proper double scrollbar frame in Tkinter

Tkinter is a Python binding to the Tk GUI(Graphical User Interface) Toolkit. It is a thin-object oriented layer on top of Tcl/Tk. When combined with Python, it helps create fast and efficient GUI applications.

Note: For more information refer, Python GUI-tkinter



Steps to Create a double scrollbar frame in Tkinter

1) Firstly, the module Tkinter will be imported as:

import tkinter as tk 

So, tkinter is abbreviated here as tk so as to make the code look cleaner and efficient.



Now, a window will be created to display:




import tkinter as tk 
  
  
window = tk.Tk()
window.geometry("250x200")

Output:

Functions to understand:

2) The next code is to assign to the scrollbars for horizontal and vertical.




SVBar = tk.Scrollbar(window)
SVBar.pack (side = tk.RIGHT,
            fill = "y")
  
SHBar = tk.Scrollbar(window, 
                     orient = tk.HORIZONTAL)
SHBar.pack (side = tk.BOTTOM, 
            fill = "x")

Output:

Functions to understand:

3) Now, make a text box for the window:




TBox = tk.Text(window,
               height = 500,
               width = 500,
               yscrollcommand = SVBar.set,
               xscrollcommand = SHBar.set
               wrap = "none")
  
TBox = tk.Text(window, 
               height = 500,
               width = 500
               yscrollcommand = SVBar.set
               xscrollcommand = SHBar.set
               wrap = "none")
  
TBox.pack(expand = 0, fill = tk.BOTH)

Functions to understand:

SHBar.config(command = TBox.xview)
SVBar.config(command = TBox.yview)

Here, within the arguments of the function config(), the scrollbars are assigned at their specific x-axis and y-axis and are able to function.

Now, insert some amount of text to display:

Num_Vertical = (“\nA\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ”)
Num_Horizontal = (“A B C D E F G H I J K L M N O P Q R S T U V W X Y Z”)

To insert the text in the window for the display, the following code is done:




TBox.insert(tk.END, Num_Horizontal)
TBox.insert(tk.END, Num_Vertical)

Complete Code:




import tkinter as tk
   
  
Num_Vertical = ("\nA\nB\nC\nD\nE\nF\nG\n\
H\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\n\
U\nV\nW\nX\nY\nZ")
Num_Horizontal = ("A  B  C  D  E  F  G  H \
I  J  K  L  M  N  O  P  Q  R  S  T  U  V \
W  X  Y  Z")
   
window = tk.Tk()
window.geometry("250x200")
   
SVBar = tk.Scrollbar(window)
SVBar.pack (side = tk.RIGHT, 
            fill = "y")
   
SHBar = tk.Scrollbar(window, 
                     orient = tk.HORIZONTAL)
SHBar.pack (side = tk.BOTTOM, 
            fill = "x")
   
TBox = tk.Text(window, 
               height = 500
               width = 500,
               yscrollcommand = SVBar.set,
               xscrollcommand = SHBar.set
               wrap = "none")
  
TBox = tk.Text(window,
               height = 500,
               width = 500,
               yscrollcommand = SVBar.set
               xscrollcommand = SHBar.set
               wrap = "none")
  
TBox.pack(expand = 0, fill = tk.BOTH)
   
TBox.insert(tk.END, Num_Horizontal)
TBox.insert(tk.END, Num_Vertical)
   
SHBar.config(command = TBox.xview)
SVBar.config(command = TBox.yview)
   
window.mainloop()

Output:


Article Tags :