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
?
The default font is 10px sans-serif.
mytext = self. canvas. create_text(100,10,fill="darkblue",font="Times 20 italic bold",text="Click the bubbles that are multiples of two.")
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 = 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With