How to Change the Tkinter Label Font Size?
Last Updated :
23 Dec, 2020
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
from tkinter import Tk
from tkinter.ttk import Label
class App:
def __init__( self , master) - > None :
self .master = master
Label( self .master, text = "I have default font-size" ).pack(pady = 20 )
Label( self .master,
text = "I have a font-size of 25" ,
font = ( "Arial" , 25 )
).pack()
if __name__ = = "__main__" :
root = Tk()
root.title( "Change font-size of Label" )
root.geometry( "400x250" )
app = App(root)
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
from tkinter import Tk
from tkinter.ttk import Label, Style
class App:
def __init__( self , master) - > None :
self .master = master
Label( self .master, text = "I have default font-size" ).pack(pady = 20 )
self .style = Style( self .master)
self .style.configure( "My.TLabel" , font = ( 'Arial' , 25 ))
Label(
self .master,
text = "I have a font-size of 25" ,
style = "My.TLabel" ).pack()
if __name__ = = "__main__" :
root = Tk()
root.title( "Change font-size of Label" )
root.geometry( "400x250" )
app = App(root)
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
from tkinter import Tk
from tkinter.ttk import Label, Style
class App:
def __init__( self , master) - > None :
self .master = master
Label( self .master, text = "I have default font-size" ).pack(pady = 20 )
self .style = Style( self .master)
self .style.configure( "TLabel" , font = ( 'Arial' , 25 ))
Label( self .master, text = "I have a font-size of 25" ).pack()
if __name__ = = "__main__" :
root = Tk()
root.title( "Change font-size of Label" )
root.geometry( "400x250" )
app = App(root)
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
from tkinter import Tk
from tkinter.font import BOLD, Font
from tkinter.ttk import Label
class App:
def __init__( self , master) - > None :
self .master = master
Label( self .master, text = "I have default font-size" ).pack(pady = 20 )
self .bold25 = Font( self .master, size = 25 , weight = BOLD)
Label( self .master, text = "I have a font-size of 25" ,
font = self .bold25).pack()
if __name__ = = "__main__" :
root = Tk()
root.title( "Change font-size of Label" )
root.geometry( "400x250" )
app = App(root)
root.mainloop()
|
Output:
Share your thoughts in the comments
Please Login to comment...