Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following code:

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

How do I set the font size with a variable named rndfont?

like image 372
carte Avatar asked Mar 17 '13 04:03

carte


People also ask

What font size does canvas use?

The default font is 10px sans-serif.

How do I change text in canvas in Python?

mytext = self. canvas. create_text(100,10,fill="darkblue",font="Times 20 italic bold",text="Click the bubbles that are multiples of two.")


2 Answers

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

canvas.create_text(x, y, font=("Purisa", rndfont), text=k)
like image 147
A. Rodas Avatar answered Oct 15 '22 14:10

A. Rodas


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 = 12
canvas.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.

like image 25
Ali Alkhatib Avatar answered Oct 15 '22 14:10

Ali Alkhatib