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
# 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, ..)
# Add Text Box text = Text(root)
text.pack() |
- Set tab size
Here will use tkfont() method from the font package
# Set Font font = tkfont.Font(font = text[ 'font' ])
# Set Tab size tab_size = font.measure( ' ' )
text.config(tabs = tab_size)
|
Below is the Implementation:
# 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: