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
from tkinter import *
root = Tk()
root.geometry( "400x400" )
root.mainloop()
|
Output:
Syntax:
T = Text(root, bg, fg, bd, height, width, font, ..)
Python3
text = Text(root)
text.pack()
|
Here will use tkfont() method from the font package
Python3
font = tkfont.Font(font = text[ 'font' ])
tab_size = font.measure( ' ' )
text.config(tabs = tab_size)
|
Below is the Implementation:
Python3
from tkinter import *
import tkinter.font as tkfont
root = Tk()
root.geometry( "400x400" )
text = Text(root)
text.pack()
font = tkfont.Font(font = text[ 'font' ])
tab_size = font.measure( ' ' )
text.config(tabs = tab_size)
root.mainloop()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...