Problem With Tkinter Font

Peter Otten __peter__ at web.de
Sun Feb 26 03:17:00 EST 2017


Wildman via Python-list wrote:

> Python 3.4.2
> Tkinter 8.6
> Linux
> 
> I want to set the font in a GUI program I am working on.
> Here is the pertinent code I am using...
> 
> from tkinter import font
> 
> myfont = font.Font(family='Helvetica', size=10, weight='bold')
> 
> Here is the error I get...
> 
> Traceback (most recent call last):
>   File "./test.py", line 41, in <module>
>     myfont = font.Font(family='Helvetica', size=10, weight="bold")
>   File "/usr/lib/python3.4/tkinter/font.py", line 93, in __init__
>     tk.call("font", "create", self.name, *font)
> AttributeError: 'NoneType' object has no attribute 'call'
> 
> From my research, the syntax is correct but I am having
> doubts.  Can anyone clarify?

If you do not provide the root argument there is still an implicit 
dependency:

"""
class Font:
...
    def __init__(self, root=None, font=None, name=None, exists=False,
                 **options):
        if not root:
            root = tkinter._default_root
        tk = getattr(root, 'tk', root)

"""

>>> import tkinter
>>> from tkinter import font
>>> font.Font(family="Helpvetica", size=10, weight="bold")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/tkinter/font.py", line 93, in __init__
    tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'call'
>>> root = tkinter.Tk()
>>> font.Font(family="Helpvetica", size=10, weight="bold")
<tkinter.font.Font object at 0x7fb9fdfdb6d8>

So you have to create the main window before you can create a Font.




More information about the Python-list mailing list