How to set the tab size in Text widget in Tkinter?

The Python Tkinter module allows us to develop graphical user interfaces (GUIs) and show main windows on the screen. The value of a Tkinter window screen from its ability to improve the user experience and enable the interactivity of user's programs. In Python, we have some built-in functions like Tk(), Text(), pack(), etc. that can be used to set the tab size in Text widget in tkinter.


The following syntax is used in the examples −


This is a built-in function of tkinter module that helps to display the main window and manage all the components of the tkinter application.


This is a built-in function of tkinter that allows users to write something on the tkinter window.


The built-in method pack() is one of several geometry managers available in Tkinter for controlling the positioning of widgets within their parent container.


This is a built-in method of tkinter module in Python that helps to set the size, position, and other screen properties.


The mainloop() function follows the tkinter module in Python which call at the end of the program.


This method works as a widget of a text entry field that allows the user to input a single line of text.

Installation Required −

pip install tkinter

This command helps to run the program based on tkinter.

Example 1

In the following example, we will start the program by importing the module named tkinter that will helps to run the program in GUI interface. Next, using the Tk() to set the main application window. To set the width and height it will use the built-in function geometry() with variable r. Using mainloop() helps to get the output of the program.

from tkinter import *
# Mention the object
r = Tk()

# Set the width and height using the built-in method geometry

# Execute Tkinter


The red arrow represents the width of the tab size whereas the blue arrow represents the height of the tab size.

Example 2

In the following example, we will show the base GUI program of Python that uses the Tkinter package to generate a GUI window. It loads the Tkinter library as well as its font module. Then it generates a Tk object called root, which serves as the main application window. Using the pack() method, a Text widget is built and it is added to the root window. To set the Text widget tab size, the font of the text widget is accessed and its size is measured. Finally, we are executing the main event loop by using the built-in function mainloop() and get the result.

from tkinter import *
import tkinter.font as tkfont
root = Tk()
text = Text(root)
font = tkfont.Font(font=text['font'])
tab_size = font.measure(' ')


Example 3

In the following example, we will show the simple GUI program that generates a graphical user interface (GUI) using the Tkinter toolkit. The program produces a 600x200 pixel window and inserts an Entry widget inside it. The Entry widget has a width of 300 pixels and a height of 150 pixels and is located at coordinates (15, 15). An Entry widget is a text entry field that allows a single line of text to be entered.

import tkinter as t

box = t.Tk()

entryExample = t.Entry(box) = 15,
   y = 15,



Example 4

In the following example, start the program with tkinter module and set the main window with the help of built-in function Tk() in the variable tab. Using title() with the tab variable it set the name of the GUI interface. Then the geometry() set the size of the main window whereas the minsize() set the minimum size of the window and it looks better by using these built-in functions. Then the sentence "All power is within you" is displayed on the label in the "Times" font at a size of 20. The Label's background colour is set to #f51655 and its text colour is set to #fcfc0a. The Label is packed into the window with the pack() method and the fill=BOTH and expand=True arguments.

from tkinter import *
tab = Tk()
tab.minsize(250, 350)
   text="All power is within you",
   font=('Times', 20),
   bg = '#f51655',
   fg = '#fcfc0a'
   ).pack(fill=BOTH, expand=True)



We discussed the various representation of tab size that appears very interactive while printing the result. Creating such type of window in Python enables interactive GUI development, improving user experience, displaying information, handling events, and supporting integration, making them essential for developing user-friendly programs.

Updated on: 17-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started