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

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


For text items, the font size is part of the font keyword argument:

canvas.create_text(x, y, font=("Purisa", rndfont), text=k)


font is an attribute which you can pass in tkinter objects. You pass a tuple indicating the font name and size, so your code should look more like:

canvas.create_text(x, y, font=("Purisa", 12), text= k)

But you're asking how to make the font size a variable. You should just be able to pass it as a variable the way you would for any other use:

rndfont = 12canvas.create_text(x, y, font=("Purisa", rndfont), text= k)

I just tested it and it seems that if you pass an invalid attribute for that tuple (like pass an empty string where the font name should be), it'll ignore the attribute entirely.


You create the font size variable:

rndfont=12

and display the text on the canvas:

canvas.create_text(x,y,font=('Pursia',rndfont),text=k)

The font parameter can be a tuple with the font name, font size, and the special effect(bold, italic...), such as:

font=('Arial',30,'bold italic')