Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to set font for Text in Tkinter?

  • Difficulty Level : Easy
  • Last Updated : 02 Dec, 2020

Prerequisite:  Python GUI – tkinter

Python provides a Tkinter module for GUI (Graphical User Interface). In this article, we are going to see how to set text font in 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

Text Widget is used where a user wants to insert multi-line text fields. In this article, we are going to learn the approaches to set the font inserted in the text fields of the text widget. It can be done with different methods.



Method 1: Using a tuple and .configure( ) method.

Approach :

  • Import the tkinter module.
  • Create a GUI window.
  • Create our text widget.
  • Create a tuple containing the specifications of the font. But while creating this tuple, the order should be maintained like this, (font_family, font_size_in_pixel, font_weight). Font_family and font_weight should be passed as a string and the font size as an integer.
  • Parse the specifications to the Text widget using .configure( ) method.

Below is the implementation of the above approach

Python3




# Import the tkinter module
import tkinter
  
# Creating the GUI window.
root = tkinter.Tk()
root.title("Welcome to GeekForGeeks"
root.geometry("400x240")
  
# Creating our text widget.
sample_text = tkinter.Text( root, height = 10)
sample_text.pack()
  
# Creating a tuple containing 
# the specifications of the font.
Font_tuple = ("Comic Sans MS", 20, "bold")
  
# Parsed the specifications to the
# Text widget using .configure( ) method.
sample_text.configure(font = Font_tuple)
root.mainloop()

Output :

Method 2: Setting the font using the Font object of tkinter.font

Approach: 

  • Import the Tkinter module.
  • Import Tkinter font.
  • Create the GUI window
  • Create our text widget.
  • Create an object of type Font from tkinter.font module. It takes in the desired font specifications(font_family, font_size_in_pixel , font_weight) as a constructor of this object. This is that specified object that the text widget requires while determining its font.
  • Parse the Font object to the Text widget using .configure( ) method.

Below is the implementation of the above approach:

Python3




# Import module
import tkinter
import tkinter.font
  
# Creating the GUI window.
root = tkinter.Tk()
root.title("Welcome to GeekForGeeks"
root.geometry("918x450")
  
# Creating our text widget.
sample_text=tkinter.Text( root, height = 10)
sample_text.pack()
  
# Create an object of type Font from tkinter.
Desired_font = tkinter.font.Font( family = "Comic Sans MS"
                                 size = 20
                                 weight = "bold")
  
# Parsed the Font object 
# to the Text widget using .configure( ) method.
sample_text.configure(font = Desired_font)
root.mainloop()

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!