[Python] Trying to create a Menu Bar Class in Tkinter but I can't get it to work

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Windows 11 and Pycharm

    I started this program, then started to switch it to OOP. So I'm starting with making a Menu Bar Class. I can't seem to get the Menu Bar to work. The rest of the code works. It's not wired to do anything yet except put some widgets on the screen. That is it works as long as I comment out lines 30 and 31 where I try to create said menu bar.

    I've seached google and youtube. I've found a few exapmples on Youtube, the web and here on Stack Overflow. I've tried everything I can think of or find over the last 3 days, nothing has worked. So now I would really like your help please.

    The error get is this...

    Traceback (most recent call last):
    File "D:\My Data\Developement\MyMoviesOOP2\.venv\Main.py", line 118, in <module>
    app = App()
    ^^^^^
    File "D:\My Data\Developement\MyMoviesOOP2\.venv\Main.py", line 30, in __init__
    self.menu_bar = MenuBarClass(self)
    ^^^^^^^^^^^^^^^^^^
    File "D:\My Data\Developement\MyMoviesOOP2\.venv\Main.py", line 5, in __init__
    super.__init__(parent)
    TypeError: descriptor '__init__' requires a 'super' object but received a 'App'

    Process finished with exit code 1


    from tkinter import *

    class MenuBarClass(Menu):
    def __init__(self, parent):
    super.__init__(parent)

    file_menu = Menu(self, tearoff=0)
    file_menu.add_command(label="New", command=new_command)
    file_menu.add_command(Label="Save...", menu=file_menu, command=save_command)
    main_menu.add_cascade(label="file", menu=file_menu)

    def new_command(self):
    pass

    def save_command(self):
    pass

    class App(Tk):
    def __init__(self):
    super().__init__()

    self.title("My Movies")
    # self.iconbitmap("images/moviereel32.ico")
    self.geometry("1200x800")

    # Create menu Bar

    self.menu_bar = MenuBarClass(self)
    self.config(menu=menu_bar)

    # Define a common font

    self.myfont = font = ("Helvetica", 10, "bold")

    # Create Frame for list of movies. Anchored to left side on main window

    self.listframe = Frame(self, width=100, height=50, bg="blue", bd=1, relief=RAISED)
    self.listframe.pack(side=LEFT)

    # Create Movie frame to hold info on selected movie. Anchored to right on main window

    self.movieframe = Frame(self, width=600, height=800, bd=5, relief=SUNKEN)
    self.movieframe.pack(side=RIGHT)

    # Create List box in list frame on left
    self.listbox = Listbox(self.listframe, width=100, height=50, font=self.myfont)
    self.listbox.pack(padx=5, pady=5)

    # Add temperary movies to list for example purposes
    self.listbox.insert(END, "The Creature From The Black Lagoon")
    self.listbox.insert(END, "The God's Must Be Crazy")

    # Create Entry Box in movie frame for title of movie

    self.titletext = Entry(self.movieframe, width=50, font=self.myfont)
    self.titletext.place(relx=0.5, rely=0.1, anchor=CENTER)

    # Insert temperary movie title for example purposes

    self.titletext.insert(0, "The Creature from the Black Lagoon")

    # Create Entry Box for movie lenght

    self.lengthtext = Entry(self.movieframe, width=10, font=self.myfont)
    self.lengthtext.place(relx=0.8, rely=0.2, anchor=W)

    self.lengthtext.insert(0, "1:34")

    # Create Entry Box for year of movie release

    self.yeartext = Entry(self.movieframe, width=4, font=self.myfont)
    self.yeartext.place(relx=0.1, rely=0.2, anchor=E)

    self.yeartext.insert(0, "1958")

    # Create Entry box for whether or not I have watched the movie

    self.watchedtext = Entry(self.movieframe, width=3, font=self.myfont)
    self.watchedtext.place(relx=0.5, rely=0.3, anchor=CENTER)

    self.watchedtext.insert(0, "Yes")

    # Buttons
    # Add Button adds a movie to the list
    # Update Button updates the information for the current selection
    # Delete Button deletes the selected movie

    self.addbutton = Button(self.movieframe, text="Add", command=self.add_movie)
    self.addbutton.place(relx=0.3, rely=0.5, anchor=W)
    self.updatebutton = Button(self.movieframe, text="Update", command=self.update_movie)
    self.updatebutton.place(relx=0.5, rely=0.5, anchor=CENTER)
    self.deletebutton = Button(self.movieframe, text="Delete", command=self.delete_movie)
    self.deletebutton.place(relx=0.7, rely=0.5, anchor=E)

    def add_movie(self):
    pass

    def update_movie(self):
    pass

    def delete_movie(self):
    pass

    # main_menu = Menu(parent)
    # parent.config(menu=main_menu)

    app = App()
    app.mainloop()

    Continue reading...

Compartilhe esta Página