Tkinter Label is used to display one or more lines, it can also be used to display bitmap or images. In this article, we are going to change the font-size of the Label Widget. To create Label use following:
Syntax: label = Label(parent, option, …)
Parameters:
parent: Object of the widget that will display this label, generally a root object
text: To display one or more lines of text.
image: To display a static image
compound: To display both Text and Image. It accepts TOP, BOTTOM, LEFT, RIGHT, CENTER. For example, if you write compound=TOPimage will displayed to the top of Text.
We can do this using different methods:
Method 1: By using Label’s font property.
Python3
# importing tkinter module and Widgets from tkinter import Tk from tkinter.ttk import Label # Creating App class which will contain # Label Widgets class App: def __init__( self , master) - > None : # Instantiating master i.e toplevel Widget self .master = master # Creating first Label i.e with default font-size Label( self .master, text = "I have default font-size" ).pack(pady = 20 ) # Creating second label # This label has a font-family of Arial # and font-size of 25 Label( self .master, text = "I have a font-size of 25" , # Changing font-size here font = ( "Arial" , 25 ) ).pack() if __name__ = = "__main__" : # Instantiating top level root = Tk() # Setting the title of the window root.title( "Change font-size of Label" ) # Setting the geometry i.e Dimensions root.geometry( "400x250" ) # Calling our App app = App(root) # Mainloop which will cause this toplevel # to run infinitely root.mainloop() |
Output:
Method 2: By using Style class. In this method, we will use our custom style otherwise all the Label widgets will get the same style.
Python3
# importing tkinter module and Widgets from tkinter import Tk from tkinter.ttk import Label, Style # Creating App class which will contain # Label Widgets class App: def __init__( self , master) - > None : # Instantiating master i.e toplevel Widget self .master = master # Creating first Label i.e with default font-size Label( self .master, text = "I have default font-size" ).pack(pady = 20 ) # Instantiating Style class self .style = Style( self .master) # Configuring Custom Style # Name of the Style is "My.TLabel" self .style.configure( "My.TLabel" , font = ( 'Arial' , 25 )) # Creating second label # This label has a font-family of Arial # and font-size of 25 Label( self .master, text = "I have a font-size of 25" , # Changing font-size using custom style style = "My.TLabel" ).pack() if __name__ = = "__main__" : # Instantiating top level root = Tk() # Setting the title of the window root.title( "Change font-size of Label" ) # Setting the geometry i.e Dimensions root.geometry( "400x250" ) # Calling our App app = App(root) # Mainloop which will cause this toplevel # to run infinitely root.mainloop() |
Output:
Note: In the above method, TLabel is the name of the default style. So if you want to create your own style name then always use below syntax
my_style_name.default_style_name
Example:
New.TButton # To override Button Widget’s styles
My.TLabel # To override Label Widget’s Styles
Abc.TEntry # To override Entry Widget’s Styles
If you use only the default style name then it will apply to all the corresponding widgets i.e if I use TLabel instead of My.TLabel then both the label will have font-size of 25. And importantly, if you use the default style name then you don’t need to provide style property.
Extra: Changing font size using the Default Style Name.
Python3
# importing tkinter module and Widgets from tkinter import Tk from tkinter.ttk import Label, Style # Creating App class which will contain # Label Widgets class App: def __init__( self , master) - > None : # Instantiating master i.e toplevel Widget self .master = master # Creating first Label i.e with default font-size Label( self .master, text = "I have default font-size" ).pack(pady = 20 ) # Instantiating Style class self .style = Style( self .master) # Changing font-size of all the Label Widget self .style.configure( "TLabel" , font = ( 'Arial' , 25 )) # Creating second label # This label has a font-family of Arial # and font-size of 25 Label( self .master, text = "I have a font-size of 25" ).pack() if __name__ = = "__main__" : # Instantiating top level root = Tk() # Setting the title of the window root.title( "Change font-size of Label" ) # Setting the geometry i.e Dimensions root.geometry( "400x250" ) # Calling our App app = App(root) # Mainloop which will cause this toplevel # to run infinitely root.mainloop() |
Notice in the above program that we have not provided style or font property to any of the Label still both of them got the same font-size and same font-family.
Output:
Method 3: By using the Font class. In this method, we will create a Font object and then use this to change Font style of any widget. Benefit of this
Font class consist of following property:
root: Object of toplevel widget.
family: The font family name as a string.
size: The font height as an integer in points.
weight: ‘bold’/BOLD for boldface, ‘normal’/NORMAL for regular weight.
slant: ‘italic’/ITALIC for italic, ‘roman’/ROMAN for unslanted.
underline: 1/True/TRUE for underlined text, 0/False/FALSE for normal.
overstrike: 1/True/TRUE for overstruck text, 0/False/FALSE for normal.
Python3
# importing tkinter module and Widgets from tkinter import Tk from tkinter.font import BOLD, Font from tkinter.ttk import Label # Creating App class which will contain Label Widgets class App: def __init__( self , master) - > None : # Instantiating master i.e toplevel Widget self .master = master # Creating first Label i.e with default font-size Label( self .master, text = "I have default font-size" ).pack(pady = 20 ) # Creating Font, with a "size of 25" and weight of BOLD self .bold25 = Font( self .master, size = 25 , weight = BOLD) # Creating second label # This label has a default font-family # and font-size of 25 Label( self .master, text = "I have a font-size of 25" , font = self .bold25).pack() if __name__ = = "__main__" : # Instantiating top level root = Tk() # Setting the title of the window root.title( "Change font-size of Label" ) # Setting the geometry i.e Dimensions root.geometry( "400x250" ) # Calling our App app = App(root) # Mainloop which will cause this toplevel # to run infinitely root.mainloop() |
Output: