5

When I run this code:

from Tkinter import *
import tkFont

class Statify():

    def __init__(self):

        ### Broken
        self.titleFont = tkFont.Font(family='Helvetica', size=24, weight='bold')
        self.option_add(*Label*font, self.titleFont)
        ###

        self.root = Tk()
        self.root.withdraw()
        self.main = Toplevel(self.root)
        self.main.title('')
        self.main_header = Frame(self.main)
        self.main_footer = Frame(self.main)
        self.main_title = Label(self.main_header, text='Statify Me v1.0 (WIP)')
        self.main_exit = Button(self.main_footer, text='Quit', command=quit)
        self.main_header.pack()
        self.main_footer.pack()
        self.main_title.pack()
        self.main_exit.pack()
        mainloop()

statify = Statify()

I get:

Traceback (most recent call last):
  File "Statify.py", line 23, in <module>
    statify = Statify()
  File "Statify.py", line 7, in __init__
    self.titleFont = tkFont.Font(family='Helvetica', size=24, weight='bold')
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/tkFont.py", line 88, in __init__
AttributeError: 'NoneType' object has no attribute 'tk'

From what I've read, this should work, and using an option file instead doesn't make a difference.

Python version 2.7.2 Tkinter verion 8.5

karthikr
  • 97,368
  • 26
  • 197
  • 188
insiduoustek
  • 53
  • 1
  • 6
  • Is this the entire code ? – karthikr Oct 22 '13 at 15:06
  • I quickly paraphrased the broken portion of a much larger application, and in doing so introduced some syntactical errors (some of which you edited - cheers!). Brionius' answer worked for me. Thanks! – insiduoustek Oct 22 '13 at 15:13

1 Answers1

7

If you look at the docs for tkFont, you'll see that the problem is that tkFont.Font requires a root argument - i.e. a parent widget. Fix this by moving the call to tkFont.Font below where you create the root window, then add self.root as a keyword argument, like so:

self.root = Tk()
self.titleFont = tkFont.Font(root=self.root, family='Helvetica', size=24, weight='bold')
                             ^^^^^^^^^^^^^^

You haven't gotten to this bug yet, but there are problems with the next line - I think you meant to write self.root.option_add rather than self.option_add, and I don't know what you're trying to do with the *Label*font business.

nbro
  • 15,395
  • 32
  • 113
  • 196
Brionius
  • 13,858
  • 3
  • 38
  • 49
  • Thank you for explaining the issue, and catching the problem I would've noticed next. I was attempting to specify font styles to use for all widgets of a certain type - in this code using Label as an example. But I intend to use an option file to do this for the whole application. Thanks for your help! – insiduoustek Oct 22 '13 at 15:16