Setting font in tkinter returns errors
Question: I'm in the middle of rewriting the code for my first tkinter application, in
which I'd avoided using classes. That was a dead end and I have to finally
learn class programming in python. I've encountered a very weird error and I
have no idea how to fix it. I've tried, but to no effect. What I'm trying to
do is specify a font for two labels in my app. It worked well in my previous,
class-free code but now it gives me an error:

    
    
    (...) line 56, in create_widgets
    TimeFont = font.Font(family='Palatino', size=88, weight='bold')
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/font.py", line 71, in __init__
    root = tkinter._default_root
    AttributeError: 'module' object has no attribute '_default_root'
    

Here's the function I'm using for creating widgets:

    
    
    def create_widgets(self):
        self.set_timer = ttk.Entry(self, textvariable=self.timer)
        self.start = ttk.Button(self, text='Start', command=self.start)
        TimeFont = font.Font(family='Palatino', size=88, weight='bold') #the infamous line 56
        self.display1 = ttk.Label(self, textvariable=self.player1, font=TimeFont)
        self.display2 = ttk.Label(self, textvariable=self.player2, font=TimeFont)
    

And some more code "from above" in case its relevant:

    
    
    from decimal import *
    from tkinter import *
    from tkinter import ttk
    from tkinter import font
    import time, _thread, subprocess
    
    class Chclock(ttk.Frame):
    
        @classmethod
        def main(cls):
            NoDefaultRoot()
            root = Tk()
            app = cls(root)
            app.grid(sticky=NSEW)
            root.grid_columnconfigure(0, weight=1)
            root.grid_rowconfigure(0, weight=1)
            root.resizable(True, False)
            root.mainloop()
    
        def __init__(self, root):
            super().__init__(root)
            root.bind('q', self.player1move)
            root.bind('p', self.player2move)
            root.bind('b', self.pause)
            root.bind('a', self.undo)
            root.bind('l', self.undo)
            self.create_variables()
            self.create_widgets() #here I call the function containing the error
            self.grid_widgets()
            self.grid_columnconfigure(0, weight=1)
    

It's probably something silly but I just can't _understand_ what's causing
this problem. It used to work fine...

Thanks!


Answer: Perhaps the code "NoDefaultRoot()" and the error message "object has no
attribute '_default_root'" might have something to do with each other? Notice
a correlation? First rule of debugging is to assume the error message is
telling you something useful.

The problem is that you are creating a font object without telling that object
what window it belongs to. Since you aren't telling it, it chooses to use the
default root window. However, you've explicitly requested no default root
window.

This is a somewhat strange way to structure your Tkinter program. I recommend
reading the answers in the question [Python Tkinter Program
Structure](http://stackoverflow.com/q/17466561/7432)

