In this article, we are going to learn how to change the font size of the text in Tkinter. Font size refers to how large the characters displayed on the screen are. It is crucial to use proper font size in order to gain the reader’s attention wherever needed. So let’s see the different ways using which we can change the font size of text using Tkinter.
Method 1: Changing Tkinter font size using the font as a tuple
import tkinter as tk from tkinter import * #main window root = Tk() #title of the window root.title("Tkinter Font Size") #adding a label l = Label(root, text="This is a sample line with font size 15.", width=40, height=5, font=('Times New Roman', 15, 'bold')) l.pack() root.mainloop()
In the above code, we have created a very basic GUI and added a label to it. Label widgets have an inbuilt property of font(‘font family’, size, ‘font style’). Font acts as a parameter in the code above. If not mentioned explicitly, the parameters will have their default values. In the above code, we have only used the size property and set the size to 15.
Method 2: Changing tkinter font size using the font as an object
Recommended Read: Tkinter Font Class Tutorial
import tkinter as tk from tkinter import * import tkinter.font as tkFont #main window root = tk.Tk() #title of the window root.title("Tkinter Font Size") #creating a font object fontObj = tkFont.Font(size=28) #adding a label l = Label(root, text="This is a sample line with font size 28.", width=40, height=5, font=fontObj) l.pack() root.mainloop()
Here, we have created an object of the Font class named fontObj and set the font size to 28. Later, in the label, we assigned the value of parameters for the font to be equal to the font object (fontObj) and hence, in the output, the font size is 28. In addition to size, we can also mention the font family and style as required.
Method 3: Using changing fonts using a custom class
import tkinter as tk from tkinter import * from tkinter.font import Font #creating a custom class class cl: def __init__(self, master) -> None: self.master = master #setting the font size to be 40 self.customFont = Font(size=40) Label(self.master, text="Font size in the custom class is 40.", font=self.customFont).pack() #end of custom class if __name__ == "__main__": #main window root = tk.Tk() #title of the window root.title("Tkinter Font Size") #adding a label l = Label(root, text="This is a sample line with default font size.", width=40, height=5) l.pack() #calling our class object customSize = cl(root) root.mainloop()
In the above example, we have defined a custom class (cl), inside which we have a constructor where we assign the font size to 40. A label is also created with the font parameter being equal to the customFont.
In the main function, we first created a label with no explicit mention of the size of the text. Hence, it has the default size. After this, we call the cl class by creating its object customSize. On creating a new instance or object, the label with font size 40 is also displayed in the output along with the default size label.
Every time a new instance of the class is created, the label with font size 40 will be displayed as it is a part of the constructor of the class here. We can also set the font family and style inside the class itself.
In this way, we have understood how to change the font size in Tkinter in multiple ways. It is an easy topic to understand and implement. Please check out our other Tkinter tutorials here.