Skip to content
Related Articles

Related Articles

Improve Article
How to set the tab size in Text widget in Tkinter?
  • Last Updated : 08 Mar, 2021

Prerequisite: Tkinter

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.

In this article, we will learn how to set the tab size in the text widget using Python in Tkinter. Here the tab size means that how many spaces will be printed after pressing the tab button. Let’s see the approach for doing the same.

Let’s Understand step by step implementation:

  • Create a Normal Tkinter window

Python3






# Import Module
from tkinter import *
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("400x400")
  
# Execute Tkinter
root.mainloop()

Output:

  • Add text widget

Syntax:

T = Text(root, bg, fg, bd, height, width, font, ..)

Python3




# Add Text Box
text = Text(root)
text.pack()
  • Set tab size

Here will use tkfont() method from the font package

Python3




# Set Font
font = tkfont.Font(font=text['font'])
  
# Set Tab size
tab_size = font.measure('        ')
text.config(tabs=tab_size)

Below is the Implementation:

Python3




# Import Module
from tkinter import *
import tkinter.font as tkfont
  
# Create Object
root = Tk()
  
# Set Geometry
root.geometry("400x400")
  
# Add Text Box
text = Text(root)
text.pack()
  
# Set Font
font = tkfont.Font(font=text['font'])
  
# Set Tab size
tab_size = font.measure('           ')
text.config(tabs=tab_size)
  
# Execute Tkinter
root.mainloop()

Output:

adjust tab size tkinter

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :