Use ScrollBar in Tkinter and Set ScrollBar in TextBox in Tkinter

If we are creating a project and want to start programming in python using tkinter and want to use scrolling in many widgets like- TextBox, Listbox, TreeView etc. Then we will use ScrollBar widget in Tkinter python.

Here we learn How to use ScrolledText in Tkinter or how can we Set Scrollbar in Textbox in Python Tkinter or How to use Tkinter ScrollBar in Python?
 
So, Lets learn Following Point wise steps.

  1. Learn about Tkinter ScrollBar in Python
  2. Change Tkinter ScrollBar Color and Style
  3. How to Set ScrollBar in Textbox in Python Tkinter


Tkinter ScrollBar in Python


When you are start programming in python, you will find very useful widget, as you know Scrollbar Widget in tkinter, you can use it in many widgets.


(1). Learn About Tkinter ScrollBar in Python :

In Python you can find that, you can use scrollbar in vertical and horizontal way.


Vertical ScrollBar in Tkinter:
 This will scroll the details or information of a widget from up to down. You can scroll down or return to scroll up for getting information given inside a widget.
Vertical ScrollBar in Python



Horizontal ScrollBar in Tkinter:
  This will scroll the details of information of a widget from left to right. You can scroll left or return to right to get information given inside that particular widget.

Horizontal ScrollBar in Tkinter



(2). Change Tkinter ScrollBar Color and Style :

For styling scrollbar, we use “ttk.Style ()”. Using Style will change the Background, DarkColor, LightColor, ArrowColor, TroughColor etc. Example is given bellow:


Note: For using style, you have to use ttk.Scrollbar in your application.


Syntax:
style = ttk.Style()
#Set Style
style.configure('SetStyleName', background='Set Color', troughcolor='Set Color', arrowcolor='Set Color', highlightcolor='Set Color') 

Vsrl.configure(style = 'SetStyleName')


Example:

from tkinter import *
from tkinter import ttk

SKForm = Tk()
SKForm.title('Tkinter Scroll TextBox')

Vsrl = ttk.Scrollbar(SKForm)
Vsrl.pack(side = RIGHT, fill = Y)

#Create Style For ScrollBar
style = ttk.Style()
#Set Style For ScrollBar
style.configure('TScrollbar', background='#E66565', troughcolor='#E0F5DA', arrowcolor='#65E6A9', highlightcolor='#E0F5DA') 

Vsrl.configure(style = 'TScrollbar')

MyTkTextBox = Text(SKForm, yscrollcommand = Vsrl.set)

MyTkTextBox.config(wrap = NONE, width = 22, height = 12)

#Add Record in TextBox in Tkinter
for ist in range(16):
    MyTkTextBox.insert(END, str(ist) + " = Set Horizontal and Vertical Scroll Bar in TextBox"+ "\n")

MyTkTextBox.pack(side = TOP, fill = "both", expand = True)

Vsrl.config(command = MyTkTextBox.yview)

SKForm.mainloop()


Add Text in TextBox in Tkinter


Output:


Tkinter ScrollBar Style in Python


(3). How to Set ScrollBar in Textbox in Python Tkinter :

If you want to set Scroll Bar in Text in Tkinter. Here we will write some sentences or multi-line text in TextBox in Tkinter. And want to scroll then we use following code for scrolling.
 
Set Vertical ScrollBar in TextBox in Tkinter:

from tkinter import *

SKForm = Tk()
SKForm.title('Vertical Scroll TextBox')

Vsrl = Scrollbar(SKForm)
Vsrl.pack(side = RIGHT, fill = Y)

MyTkTextBox = Text(SKForm,  yscrollcommand = Vsrl.set)
MyTkTextBox.config(wrap = NONE, width = 22, height = 12)

for ist in range(16):
    MyTkTextBox.insert(END, str(ist) +" = Set Vertical Scroll Bar in TextBox"+ "\n")

MyTkTextBox.pack(side=TOP, fill="both", expand=True)

# Set Vertical Scroll Bar to configure Text Y View
Vsrl.config(command = MyTkTextBox.yview)

SKForm.mainloop()


Now, you will get the given output.

 
Set Vertical Scroll Bar in TextBox Tkinter



Using ScrolledText in Tkinter for Vertical Scrolling:

When you will use ScrolledText Widget in Tkinter, there is no need for add extra Scroll widget in Tkinter for Vertical Scrolling. ScrolledText Widget has inbuilt Vertical Scrollbar as describe in bellow python code:
from tkinter import *
from tkinter.scrolledtext import ScrolledText

SKForm = Tk()
SKForm.title('ScrolledText in Tkinter')

skt = ScrolledText(SKForm)
skt.config(width = 39, height = 6)

#Add Text in ScrolledText in Tkinter
for ist in range(8):
    skt.insert(END, str(ist) +" = Using ScrolledText Widget Scroll "+ "\n")
    
skt.pack()

SKForm.mainloop()


Now, let’s see the output:

ScrolledText in Tkinter



(c) Set Vertical and Horizontal ScrollBar in TextBox in Tkinter:

Here we use Both Vertical and Horizontal ScrollBar in Text widget in Tkinter. Or here we set ScrollBar Orientation in Tkinter.
 

Horizontal ScrollBar in Text in Tkinter


Example:

from tkinter import *

SKForm = Tk()
SKForm.title('Tkinter Scroll TextBox')

#Create Horizontal ScrollBar in Tkinter
Hsrl = Scrollbar(SKForm, orient = 'horizontal')
Hsrl.pack(side = BOTTOM, fill = X)

#Create Vertical ScrollBar in Tkinter
Vsrl = Scrollbar(SKForm)
Vsrl.pack(side = RIGHT, fill = Y)

#Create TextBox and Set Scroll Bar
MyTkTextBox = Text(SKForm, xscrollcommand = Hsrl.set, yscrollcommand = Vsrl.set)

MyTkTextBox.config(wrap = NONE, width = 22, height = 12)

for ist in range(16):
    MyTkTextBox.insert(END, str(ist) +" = Set Horizontal and Vertical Scroll Bar in TextBox"+ "\n")

#Set TextBox widget to Form at top
MyTkTextBox.pack(side=TOP, fill="both", expand=True)

# Set Horizontal Scroll Bar to configure Textox X View
Hsrl.config(command = MyTkTextBox.xview)

# Set Vertical Scroll Bar to configure Textox Y View
Vsrl.config(command = MyTkTextBox.yview)

SKForm.mainloop()


Output:

Horizontal and Vertical Scrollbar in Tkinter

After running application, this will show above given output, you can easily scroll your Textbox content left to right or Up and down.

So, this way SKOTechLearn describe the way to use ScrollBar in Tkinter Python or you can set ScrollBar in Text Widget in Tkinter.


0 comments: