Home > OS >  What am I doing wrong with this Tkinter formatting?
What am I doing wrong with this Tkinter formatting?

Time:10-17

I am trying to create a Tkinter application using much cleaner formatting using inspiration from Bryan Oakley's suggestion from this post.

import tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # Set start-up screen width and height
        screen_width = self.parent.winfo_screenwidth()
        screen_height = self.parent.winfo_screenheight()
        self.parent.geometry(f"{screen_width}x{screen_height}")

        # Adding Header
        self.header = Header(self)
        self.header.pack(side="top", fill="x")

class Header(tk.Frame):
    def __init__(self, parent):
        tk.Frame(parent, height=50, bg="#000000")



if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

However, when running this code, I get this error:

AttributeError: 'Header' object has no attribute 'tk'

What am I doing wrong?

CodePudding user response:

Your Header class isn't properly inheriting from tk.Frame. You need to make sure the __init__ method of the base class is called just like you do in MainApplication.

class Header(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        ...

CodePudding user response:

tk.Frame in the Header class has no impact because the frame is not initialized with the class (tk.Frame) you are inheriting. hence when you use self.header.pack(side="top", fill="x") you are getting the error.You can use the below method which creates a frame object and places it in the init method. or you can add one more method inside the Header class which will return the frame object, thereby you can place it using pack in your MainApplication class

import tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # Set start-up screen width and height
        screen_width = self.parent.winfo_screenwidth()
        screen_height = self.parent.winfo_screenheight()
        self.parent.geometry(f"{screen_width}x{screen_height}")
        Header(self.parent)


class Header(object):
    def __init__(self, parent):
        self.parent = parent
        self.frame = tk.Frame(self.parent, height=50, bg="#000000")
        self.frame.pack(side="top", fill="x")



if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
  • Related