How to set the font size of a Tkinter Canvas text item?

PythonTkinterServer Side ProgrammingProgramming

Canvas is one of the flexible widgets in tkinter which controls the widgets with its power of functions, methods, and attributes. However, tkinter canvas can be used to create text using the create_text(options) constructor. We can define the text along with other properties in the constructor. After defining the text, we can control over the text style such as font-family, font-size and font-style by using font(property).

Example

Let us have a look at the following example where we will add a new text and then resize it using the font property.

#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Creating a canvas
canvas= Canvas(win, width= 430, height= 450)
#Create a text inside canvas
text= canvas.create_text(200,40,text="Hey, Developers!", font=('Helvetica','30','bold'))
canvas.pack()
win.mainloop()

Output

Running the above code will display a window that contains a text in the canvas. We can resize the text in the canvas by using the font(options) attribute.

Now, change the font size from the property and run the code to see the reflected changes.

raja
Published on 22-Apr-2021 07:35:14
Advertisements