How to Add Columns and Items in TreeView in Python Tkinter

 If you want to show items in rows and columns like table, tkinter provide Treeview widgets in python. This widget is use as Listview for display records. So here we learn Listview in Python or How to add Column Hearder in Treeview in Python Tkinter or How to add items in Treeview in Tkinter python

Now, we will cover following points that will solve all given questions.


  1. Add Column in TreeView in Tkinter Python
  2. Set Column width in TreeView in Tkinter
  3. Set Treeview Column color in tkinter
  4. Add Items in Treeview in Tkinter
  5. Tkintter Treeview Row Color in Python


So, here we use Treeview as Listview in Tkinter with following way:

(1). Add Column in TreeView in Tkinter Python :

Now, let’s see how we add columns with names with different ways in python.

Fist Way:
from tkinter import *
from tkinter import ttk

TrvFrm = Tk()

MyTrv = ttk.Treeview(TrvFrm, columns=('Column1', 'Column2', 'Column3'), show='headings')

MyTrv.heading('MyCl1', text='SKID')
MyTrv.heading('MyCl2', text='SKName')
MyTrv.heading('MyCl3', text='SKFName')


Second Way:
from tkinter import *
from tkinter import ttk

TrvFrm = Tk()

MyTrv = ttk.Treeview(TrvFrm, show='headings')
MyTrv['columns']=('MyCl1', 'MyCl2', 'MyCl3')

MyTrv.heading('MyCl1', text='SKID')
MyTrv.heading('MyCl2', text='SKName')
MyTrv.heading('MyCl3', text='SKFName')

You can choose any one of them and proceed for further Process, So, Let’s see what will happen?

Show Columns in TreeView in Tkinter


Now, here we will put complete code with output as follows:
from tkinter import *
from tkinter import ttk

TrvFrm = Tk()
TrvFrm.title('TreeView Column Header')
TrvFrm.geometry('305x110')

MyTrv = ttk.Treeview(TrvFrm, columns=('MyCl1', 'MyCl2', 'MyCl3'), show='headings')

MyTrv.heading('MyCl1', text='SKID')
MyTrv.heading('MyCl2', text='SKName')
MyTrv.heading('MyCl3', text='SKFName')

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

TrvFrm.mainloop()


When you run this code, it will show Form including Treeview with columns in python.

Add_Column_Header_in_TreeView_Tkinter


(2). Set Column width in TreeView in Tkinter :

If you want to set or fix Column width at run time then you have to write code like:

 
MyTrv.column('MyCl1', width=40)
MyTrv.column('MyCl2', width=50)
MyTrv.column('MyCl3', width=70)


Ok, we will insert this code in our existing above code.
 
from tkinter import *
from tkinter import ttk

TrvFrm  = Tk()
TrvFrm .title('TreeView Column Header')
TrvFrm .geometry('300x100')

MyTrv = ttk.Treeview(TrvFrm, show='headings')

#Create Column
MyTrv['columns']=('MyCl1', 'MyCl2', 'MyCl3')

#Set Column Name
MyTrv.heading('MyCl1', text='SKID')
MyTrv.heading('MyCl2', text='SKName')
MyTrv.heading('MyCl3', text='SKFName')

#Set Column Width
MyTrv.column('MyCl1', width=40)
MyTrv.column('MyCl2', width=50)
MyTrv.column('MyCl3', width=70)

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

TrvFrm.mainloop()


After running this code her we find output:

Set_Column_Width_in_Treeview_in_Tkinter

As you can see, we adjust the treeview Column width in tkinter according to need.



(3). Set Treeview Column color in Tkinter :

If you want to change Tkinter Treeview column color like Background color or foreground color and also want to change Treeview Column Font size in Tkinter or Treeview Column Font Name in Tkinter. Then just add Tkinter ttk style on your code.

mytkstyle = ttk.Style()
#Treeview Style in tkinter with Column Color with Font Size and Font Name
mytkstyle.configure("Trstyle.Treeview.Heading", background='#ffedd4', foreground='#f38755', font=('Georgia', 10,'bold'))

#Add ttk style on treeview
MyTrv = ttk.Treeview(TrvFrm, show='headings', style="Trstyle.Treeview")



Now, just add above given code and implement code like bellow given example:
 
from tkinter import *
from tkinter import ttk

TrRoot = Tk()
TrRoot.title('TreeView Column Header')
TrRoot.geometry('300x100')

mytkstyle = ttk.Style()

mytkstyle.configure("Trstyle.Treeview.Heading", background='#ffedd4', foreground='#f38755', font=('Georgia', 10,'bold'))

TkTree = ttk.Treeview(TrRoot, show='headings', style="Trstyle.Treeview")
TkTree['columns']=('MyCl1', 'MyCl2', 'MyCl3')

TkTree.heading('MyCl1', text='SKID')
TkTree.heading('MyCl2', text='SKName')
TkTree.heading('MyCl3', text='SKFName')

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

TrRoot.mainloop()



Now the above code is ready for display following output with different color of width of column after executing code:

Set_Column_Color in_Treeview and Font_in_Treeview_in_Tkinter



Add items in Treeview in Tkinter Python

Add items in Listview in Tkinter



(4). Add Items in Treeview in Tkinter :

Here we add some items or records in Treeview as Listview in Tkinter using ‘insert’ command. You can add items manually or through Loop as describe bellow:

(a) Add Items in Treeview in Tkinter manually :

from tkinter import *
from tkinter import ttk

TrvFrm  = Tk()
TrvFrm .title('TreeView Column Header')
Trv = ttk.Treeview(TrvFrm, show='headings')
Trv['columns']=('Clm1', 'Clm2', 'Clm3')

Trv.heading('Clm1', text='SKID')
Trv.heading('Clm2', text='SKName')
Trv.heading('Clm3', text='SKFName')

Trv.insert('', END, values=('1', 'SKO', 'AAAAAAA'))
Trv.insert('', END, values=('2', 'Tech', 'BBBBBB'))
Trv.insert('', END, values=('3', 'Learn', 'CCCCCCCC'))


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

TrvFrm.mainloop()



Output:

Manually_Add_Items_in_Treeview_in_Tkinter


(b)  Add Items in Treeview in Tkinter through Loop :

Suppose you want to add some items through loop in Treeview then here we use 'for' loop in python for insert records in treeview tkinter.


from tkinter import *
from tkinter import ttk

TrvFrm  = Tk()
TrvFrm .title('TreeView Add Itmes through Loop')

MyTrv = ttk.Treeview(TrvFrm, columns=('MyCl1', 'MyCl2', 'MyCl3'), show='headings')

MyTrv.heading('MyCl1', text='CLID')
MyTrv.heading('MyCl2', text='CLName')
MyTrv.heading('MyCl3', text='CLFName')

for sn in range(1, 6):
    MyTrv.insert('', END, values=('MyIDs'+ str(sn), 'MyName'+ str(sn), 'FName'+ str(sn)))

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

TrvFrm.mainloop()



Output:

Loop_To_Add_Items_in_Treeview_in_Tkinter


(5). Tkinter Treeview Row Color in Python :

Now, the next step to add color in rows according to desire. For it, we will use, 'ttk.Style' for color and 'tag_configure' for treeview widget for apply color. Now here we use following code for better look of treeview as Table in tkinter.

ttk style in Treeview in tkinter

Example:
from tkinter import *
from tkinter import ttk

TrvFrm = Tk()
TrvFrm.title('TreeView Row Color')
TrvFrm.geometry('300x150')

style = ttk.Style(TrvFrm)

# ttk Style theme
style.theme_use("default")

# Set treeview Row Colomn Color
style.map('Treeview', background=[('selected', '#BFBFBF')])

MyTrv = ttk.Treeview(TrvFrm, columns=('MyCl1', 'MyCl2', 'MyCl3'), show='headings', selectmode = 'browse')
MyTrv.column("#0", width=60, minwidth=30, stretch=NO)
MyTrv.column("MyCl1", width=80, minwidth=20, stretch=NO)
MyTrv.column("MyCl2", width=100, minwidth=30, stretch=NO)
MyTrv.column("MyCl3", width=100, minwidth=30, stretch=NO)

MyTrv.heading('MyCl1', text='CLID')
MyTrv.heading('MyCl2', text='CLName')
MyTrv.heading('MyCl3', text='CLFName')

# Set treeview row font color or foreground color
MyTrv.tag_configure('Cl1', background = '#f9fbdb', foreground = '#c26981')
MyTrv.tag_configure('Cl2', background = '#fbdbf1', foreground = '#6f9594')

for sn in range(1, 6):
    if (sn % 2) == 0:  
        MyTrv.insert('', END, values=('MyIDs' + str(sn), 'MyName' + str(sn), 'FName' + str(sn)), tags = 'Cl1')
    else:  
        MyTrv.insert('', END, values=('MyIDs' + str(sn), 'MyName' + str(sn), 'FName' + str(sn)), tags = 'Cl2')

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

TrvFrm.mainloop()

After that, we will find a colorful listview or treeview as given in following output:

tkinter Treeview Row Color


These are the tips and way to show data in treeview in tkinter. And you can choose color of tkinter treeview according to need for your project. If you follow above given point then you can create an application with showing data in table model with desire color. So, this is the way to Add Columns in treeview tkinter or Add items in Treeview in Tkinter or Style treeview in tkinter.

Style and Use Entry Box in tkinter and TextBox in tkinter

If you want to use Entry Box in tkinter and TextBox in tkinter or you can say that tkinter Entry Widget and tkinter Text Widget. So, here we describe following way to use or Style EntryBox or Style TextBox in tkinter. The Text or Entry widget Style will be defined in following points.
 
  1. Create and Set Location and Size of Entry or Text Widget in tkinter
  2. Set Color of Entry or Text in tkinter
  3. Set Selection Background and Foreground Color of Entry or Text in tkinter
  4. Text or Entry Box Font Setting in tkinter
  5. Text or Entry Border Style in tkinter


Tkinter Entry Widget Style in Python

Now, here we learn interesting way to style widget in tkiter.


(1). Create and Set Location and Size of Entry or Text Widget in tkinter :

Now, if you want to set particular control or widget in desire location of window and want to set Entry box Height and Width in tkinter then we use “place” method in following way:

from tkinter import *  

TkFrm = Tk()
TkFrm.geometry("250x200+500+400")
TkFrm.title('Entry Box or Text Box Location And Size in Tkinter')
#Create Entry Box
MyEntryBox1 = Entry(TkFrm)
#Set Entry Widget Location and Size in Form
MyEntryBox1.place(x=14, y=30,  width=256, height= 35)
#Add Text to Entry Widget in Python
MyEntryBox1.insert(END, 'SKOTechLearn')

#Create Entry Box
MyTxtBox = Text(TkFrm)
#Set Text Widget Location and Size in Form
MyTxtBox.place(x=14, y=90, width=256, height=50)
#Add Text to Text Widget in Python
MyTxtBox.insert(END, 'SKOTechLearn')

TkFrm.mainloop()



Here we add some text in both widgets in python tkinter. And after running it will show output:

Tkinter Entry Size and Location


This way we can increase or decrease height and width of tkinter widgets.
 

(2). Set Color of Entry or Text in tkinter :

Here, we set Background, Foreground, Border Selection color or Entry Box or Text Box in Tkinter.

Following method will use for color setting in tkinter:
MethodDetails
bg or background Use for Background color of Widgets
Fg or foreground Use for Font color of widgets
highlightbackground This method is use for Border color
highlightcolor This method is use for border color when focus on entry or text widget.


Entry And Text Widget Color Setting in tkinter:

When you want to apply border color change when lost focus and got focus, and want to change font and back color, you have to apply and write following code:

from tkinter import *  

TkFrm = Tk()
TkFrm.geometry('290x200+500+300')
TkFrm.title('Entry Box or Text Box Color Setting in Tkinter')

#Set Entry Widget Color and Border Color
MyEntryBox1 = Entry(TkFrm, highlightcolor = 'green', bg='#F3FEFF', fg='red', highlightbackground='red')

#Set Widget Location and Size in Tkinter
MyEntryBox1.place(x=14, y=30,  width=256, height= 35)
#Add Text to Entry Widget in Python
MyEntryBox1.insert(END, 'SKOTechLearn')

# Text Widget Color and border Color in Tkinter
MyTxtBox = Text(TkFrm, highlightcolor = 'blue', highlightbackground='brown', fg='Navy', bg='#FFEFE7')
#Set Widget Location and Size in Form
MyTxtBox.place(x=14, y=90, width=256, height=50)
#Add Text to Text Widget in Python
MyTxtBox.insert(END, 'SKOTechLearn')

TkFrm.mainloop()


When you execute this code, and when you focus on widget, it will change the border color as describe bellow image.

Set Tkinter Entry widget Color in Python



(3). Set Selection Background and Selection Foreground Color of Entry or Text in tkinter :

If you want to change Selection Color, when you select String or Value in Entry or Text Box, then you have to use ‘selectbackground’ for selection back color and ‘selectforeground’ for selection font color.

from tkinter import *  

TkFrm = Tk()
TkFrm.geometry('290x200+500+300')
TkFrm.title('Entry Box or Text Box Selection Color in Tkinter')

#Selection Color Setting in Tkinter Entry Widget
MyEntryBox1 = Entry(TkFrm,  selectbackground='yellow', selectforeground='green')
MyEntryBox1.place(x=14, y=30,  width=256, height= 35)
MyEntryBox1.insert(END, 'SKOTechLearn')

#Selection Color Setting in Tkinter Text Widget
MyTxtBox = Text(TkFrm, selectforeground='yellow', selectbackground='red')
MyTxtBox.place(x=14, y=90, width=256, height=50)
MyTxtBox.insert(END, 'SKOTechLearn')

TkFrm.mainloop()


Output:

Tkinter Entry Selection Color in Python



(4). Text or Entry Box Font Setting in tkinter :

Now, lets see the code, how to change font in Entry widget in tkinter or change font in Text widget in tkinter. Or Change the font size in tkinter widgets.

from tkinter import *  

TkFrm = Tk()
TkFrm.geometry('290x200+500+300')
TkFrm.title('Entry Box or Text Box Font Setting in Tkinter')

#Font Settings in tkinter
MyEntryBox1 = Entry(TkFrm,  font='Verdana 12 bold')
MyEntryBox1.pack()
MyEntryBox1.insert(END, 'SKOTechLearn')

#Font Settings in tkinter
MyTxtBox = Text(TkFrm, font='Georgia 13 italic bold')
MyTxtBox.pack()
MyTxtBox.insert(END, 'SKOTechLearn')

TkFrm.mainloop()


Output:

Entry widget Font Setting in Tkinter



(5). Text or Entry Border Style in tkinter :

Now, let’s find the last point to change Border Style in tkinter widgets. Can use following method in you code. You can change style of Text Widget Border in tkinter or Entry Widget Border in Tkinter.

MethodDetails
relief Change Border Style (raised, sunken, solid, flat, ridge, groove)
highlightthickness Change Border width when widget gain focus
borderwidth Change Border Width


Lets find code:

from tkinter import *  

TkFrm = Tk()
TkFrm.geometry('290x200+500+300')
TkFrm.title('Entry Box or Text Box Border Setting in Tkinter')

#Tkinter Widgets Border Setting
MyEntryBox1 = Entry(TkFrm,  relief='raised', highlightthickness=1, borderwidth=2)
MyEntryBox1.pack()
MyEntryBox1.insert(END, 'SKOTechLearn')

#Tkinter Widgets Border Style
MyTxtBox = Text(TkFrm, relief='sunken', highlightthickness=1, borderwidth=2)
MyTxtBox.pack()
MyTxtBox.insert(END, 'SKOTechLearn')

TkFrm.mainloop()


Output:

Tkinter Entry Border Style

So, this way you can change Enty Widget Style in Tkinter or Text Widget Style in Tkinter.

PySide Center Screen and Window State Maximized in PySide

As you know PySide provide stylish GUI comparing Tkinter and feature rich QT base application. So first, here we learn how to set Center Screen window in Pyside in Python? And also we learn Window State Maximize in Pyside in Python. Here we also include how to Set window title in PySide?

Let’s find it in following points:

  1. Set window Size in PySide6 and Set Window Location in PySide6
  2. Python Center Screen Window in PySide6
  3. Set Window State Maximized in PySide6
  4. Set Window Title in PySide6


Window State Maximize in PySide


How to Set Center Screen in Tkinter and Set window size in Tkinter?

(1). Set Window Size in PySide6 and Set Window Location in Pyside6 :

Suppose, you want to create a Form using PySide and want to display at run time with specific size with specific location on screen. Then here PySide provide ‘setGeometry’ or ‘move’ and ‘resize’ function. The syntax is used in following way:


‘setGeometry’ Syntax:
MainForm.setGeometry(Form_Left, Form_Top, Form_Width, Form_Height)

Example:
from PySide6.QtWidgets import QMainWindow, QApplication

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form's Location and Size
MyPyForm.setGeometry(150, 100, 330, 250)

MyPyForm.show()

SKapp.exec()




‘move’ Syntax:
MainForm.move(Left_Location, Top_Location)


‘resize’ Syntax:
MainForm.resize(Form_Width, Form_Height)


Now, we are using both function in given example:
from PySide6.QtWidgets import QMainWindow, QApplication

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Location of Form in PySide
MyPyForm.move(150, 100)
#Set Form Size in PySide
MyPyForm.resize(330, 250)

MyPyForm.show()

SKapp.exec()


Now, you will get same output when run application like bellow:

Set Window Size and Location in Pyside



(2). Python Center Screen Window in PySide6 :

In this point, we learn how to set start position for form in center screen. For center screen form in Pyside we have to import following:
from PySide6.QtGui import QScreen

Here are following two ways from which you can set Center Screen in Python.

Note: Remember one thing before positioning form in center screen, you have to first define the form or dialog size then write center screen code.

First we use ‘QScreen.availableGeometry(QApplication.primaryScreen())’. This will show screen available geometry.

PySide Center Screen Example 1:
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form Size
MyPyForm.resize(330, 250)

#Get Screen geometry
SrcSize = QScreen.availableGeometry(QApplication.primaryScreen())
#Set X Position Center
frmX = (SrcSize.width() - MyPyForm.width())/2
#Set Y Position Center
frmY = (SrcSize.height() - MyPyForm.height())/2
#Set Form's Center Location
MyPyForm.move(frmX, frmY)

MyPyForm.show()

SKapp.exec()


Pass Value from Text To Label in Tkinter.


Pyside Center Screen Example 2:
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form's Location and Size
MyPyForm.resize(330, 250)

#Get Screen Center Position
Fmcenter = QScreen.availableGeometry(QApplication.primaryScreen()).center()
Fmscr = MyPyForm.frameGeometry()

#Set Form in Center Position
Fmscr.moveCenter(Fmcenter)

MyPyForm.show()

SKapp.exec()



Both codes will show same output:
Pyside Center Screen Window





(3). Set Window State Maximized in Pyside6 :

Here SKOTechLearn describe two way to set window state maximized in PySide, and both will show same output as described bellow:

PySide6 Maximized Form Example 1:

from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

SKapp = QApplication()
MyPyForm = QMainWindow()

#Set Form's Location and Size
MyPyForm.resize(330, 250)

SrcSize = QScreen.availableGeometry(QApplication.primaryScreen())

#Get Screen Width
frmX=SrcSize.width()

#Get Screen Height
frmY=SrcSize.height()

#Pyside Window State maximized
MyPyForm.setGeometry(0, 0, frmX, frmY)

MyPyForm.show()

SKapp.exec()



PySide6 Maximize Form Example 2:

import sys
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QScreen

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'PySide6 Maximize Window State'
        
        #Pyside6 Maximize Form
        MyScn = QScreen
        self.setGeometry(0, 0, MyScn.availableGeometry(QApplication.primaryScreen()).width(), MyScn.availableGeometry(QApplication.primaryScreen()).height())  
    
        self.show()    
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec())


You will get maximized form:
PySide Maximize Window State




(4). Set Window Title in PySide6 :

The simple way to set window title or form title in PySide using ‘setWindowTitle’. Her we use it with two way at design time and change Form title in PySide at run time.


(i). Set Window Title at design time :
import sys
from PySide6.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)
mainwindow = QMainWindow()

mainwindow.setGeometry(300, 300, 300, 150)

#Set Window Title in PySide
mainwindow.setWindowTitle('PySide Set Window Title')

mainwindow.show()

app.exec()   



(ii). Set Window Title at run time :

Here we use ‘QPushButton’ to change Title at run time when we click on button.
import sys
from PySide6.QtWidgets import QMainWindow, QPushButton, QApplication

class MyTitle(QMainWindow):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
    def initUI(self):      

        Pysbtn = QPushButton('Show title', self)
        Pysbtn.move(70, 40)
        
        #Call Click Event which is defined bellow
        Pysbtn.clicked.connect(self.ClickToChangeTitle)
        
        self.setGeometry(300, 300, 300, 150)
        self.show()


    #QPushButton Click Event in PySide    
    def ClickToChangeTitle(self):
            self.setWindowTitle('Click to Change Title in Pyside')
        
if __name__ == '__main__':  
    app = QApplication(sys.argv)
    ex = MyTitle()
    sys.exit(app.exec())


Output:
Set Window Title in PySide


Set Window Title in Python


Now here are some Pyside windows tips, this way you can easily Maximized Window in PySide and Also Set Center Screen Window in PySide in Python. And Also Set Window Title or Form Title in PySide.

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.