Saturday, November 16, 2024
Google search engine
HomeLanguagesScrollable Frames in Tkinter

Scrollable Frames in Tkinter

A scrollbar is a widget that is useful to scroll the text in another widget. For example, the text in Text, Canvas Frame or Listbox can be scrolled from top to bottom or left to right using scrollbars. There are two types of scrollbars. They are horizontal and vertical. The horizontal scrollbar is useful to view the text from left to right. The vertical scrollbar is useful to scroll the text from top to bottom.
Now let us see how we can create a scrollbar. To create a scrollbar we have to create a scrollbar class object as: 

h = Scrollbar(root, orient='horizontal')

Here h represents the scrollbar object which is created as a child to root window. Here orient indicates horizontal for horizontal scrollbar and vertical indicates vertical scroll bars. Similarly to create a vertical scrollbar we can write: 

v = Scrollbar(root, orient='vertical')

On attaching the scrollbar to a widget like text box, list box, etc we then have to add a command xview for horizontal scrollbar and yview for the vertical scrollbar. 

h.config(command=t.xview) #for horizontal scrollbar
v.config(command=t.yview) #for vertical scrollbar

Now let us look at the code for attaching a vertical and horizontal scrollbar to a Text Widget. 

Python3




# Python Program to make a scrollable frame
# using Tkinter
  
from tkinter import *
  
class ScrollBar:
     
    # constructor
    def __init__(self):
         
        # create root window
        root = Tk()
  
        # create a horizontal scrollbar by
        # setting orient to horizontal
        h = Scrollbar(root, orient = 'horizontal')
  
        # attach Scrollbar to root window at
        # the bootom
        h.pack(side = BOTTOM, fill = X)
  
        # create a vertical scrollbar-no need
        # to write orient as it is by
        # default vertical
        v = Scrollbar(root)
  
        # attach Scrollbar to root window on
        # the side
        v.pack(side = RIGHT, fill = Y)
          
  
        # create a Text widget with 15 chars
        # width and 15 lines height
        # here xscrollcomannd is used to attach Text
        # widget to the horizontal scrollbar
        # here yscrollcomannd is used to attach Text
        # widget to the vertical scrollbar
        t = Text(root, width = 15, height = 15, wrap = NONE,
                 xscrollcommand = h.set,
                 yscrollcommand = v.set)
  
        # insert some text into the text widget
        for i in range(20):
            t.insert(END,"this is some text\n")
  
        # attach Text widget to root window at top
        t.pack(side=TOP, fill=X)
  
        # here command represents the method to
        # be executed xview is executed on
        # object 't' Here t may represent any
        # widget
        h.config(command=t.xview)
  
        # here command represents the method to
        # be executed yview is executed on
        # object 't' Here t may represent any
        # widget
        v.config(command=t.yview)
  
        # the root window handles the mouse
        # click event
        root.mainloop()
 
# create an object to Scrollbar class
s = ScrollBar()
        


Output:

RELATED ARTICLES

Most Popular

Recent Comments