Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter.font.Font() does not work

Why does this not work? I have the Consolas font on my computer but the below code only uses the default font. It seems the only font which works is the Courier font, installed with tkinter.

font_consolas = tkinter.font.Font(root, family="Consolas")

Running it with exists=True shows the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    font_consolas = tkinter.font.Font(root, family="Consolas", exists=True)
  File "C:\Python33\lib\tkinter\font.py", line 86, in __init__
    "named font %s does not already exist" % (self.name,))
_tkinter.TclError: named font font1 does not already exist

I do have the font installed and it is verified by

'Consolas' in tkinter.font.families() == True

Specifying the font in a list works, though

font_consolas = ["Consolas", ]
like image 249
kiri Avatar asked Apr 11 '26 01:04

kiri


1 Answers

According to the documentation, Font(...) will throw an error if the assertion (exists=True) is false. The assertion is based on the name of the new font, not the name of the font you're basing it on.

It's not throwing the error because the Consolas font doesn't exist, it's that it's trying to create a new font with a new name, and that new font does not exist. Since you don't give it a name, it will pick a unique name, and since by definition a unique name won't previously exist, you get the error. In effect you're saying "create a unique name for my font, and throw an error if this unique name isn't unique"

In other words, it's doing what it's documented to do.

like image 67
Bryan Oakley Avatar answered Apr 13 '26 15:04

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!