how classes can inherit tk.frames when being called as a module in python

74 Views Asked by At

I am a tk newbie and am having trouble importing classes and having them inherit frames. If I stack all the code in one file it works. Something like (I found this on Github sorry for no attribution),

imports...

class Clock(Frame):
    def __init__(self, parent, *args, **kwargs):
        Frame.__init__(self, parent, bg='black')
        code....
class FullscreenWindow:
    def __init__(self):
        self.tk = Tk()
        self.tk.configure(background='black')
        self.topFrame = Frame(self.tk, background = 'black')
        self.bottomFrame = Frame(self.tk, background = 'black')
        self.topFrame.pack(side = TOP, fill=BOTH, expand = YES)
        self.bottomFrame.pack(side = BOTTOM, fill=BOTH, expand = YES)
        self.state = False
        self.tk.bind("<Return>", self.toggle_fullscreen)
        self.tk.bind("<Escape>", self.end_fullscreen)
        # clock
        self.clock = Clock(self.topFrame)

w = FullscreenWindow()
w.tk.mainloop()

However, if I try to break up the code so I can import modules in directories and subdirectories I get errors because I do not understand how to pass the frames. For example, if I put the Clock class in a file modules/clock/Clock.py (yes I am including __init__.py in all directories) and change the code to

from modules.clock import Clock

class FullscreenWindow:
        def __init__(self):
            self.tk = Tk()
            self.tk.configure(background='black')
            self.topFrame = Frame(self.tk, background = 'black')
            self.bottomFrame = Frame(self.tk, background = 'black')
            self.topFrame.pack(side = TOP, fill=BOTH, expand = YES)
            self.bottomFrame.pack(side = BOTTOM, fill=BOTH, expand = YES)
            self.state = False
            self.tk.bind("<Return>", self.toggle_fullscreen)
            self.tk.bind("<Escape>", self.end_fullscreen)
            # clock
            self.clock = Clock(self.topFrame)

w = FullscreenWindow()
w.tk.mainloop()

I get the error

  File "modules\clock\Clock.py", line 8, in <module>
    class Clock(Frame):

NameError: name 'Frame' is not defined

How can I set up the code so I can break up code into modules and pass the tk.Frame?

0

There are 0 best solutions below