0

I'm trying to make a little program by using classes. So far, I've made two classes, in which the first one will run the next one. When I run this, I get an error message. I don't understand what's wrong, but it looks like it has something do to about that I define the name Menu1 before it's been read. I'm going to create a new function after these classes, that'll first run MainWindow, and then Menu1. I would appreciate help.

Code:

class MainWindow:
    app = Tk()
    app.title("MyApp")
    window = Frame(app, width=1050, height=550)
    app.minsize(width=1050, height=550)
    window.pack()
    menu = Menu1()
    menu.makeMenu()
    app.mainloop()


class Menu1:
    def makeMenu(self):
        app.config(menu=menu)
        menu.add_cascade(label="Settings", menu=subMenu)
        subMenu.add_command(label="Settings", command=settings1)

def settings1():
    print("Open new window")


if __name__ == "__main__":
    MainWindow()

Error message:

Traceback (most recent call last):
  File "", line 7, in <module>
    class MainWindow:
  File "", line 13, in MainWindow
    menu = Menu1()
NameError: name 'Menu1' is not defined

Process finished with exit code 1
0

1 Answer 1

5

Everything under class MainWindow is run immediately. It is not in a method. At that point class Menu1 has not yet been executed and no class by that name exists yet.

It looks like you really only wanted MainWindow to be a function instead:

def main_window():
    app = Tk()
    app.title("MyApp")
    window = Frame(app, width=1050, height=550)
    app.minsize(width=1050, height=550)
    window.pack()
    menu = Menu1()
    menu.makeMenu()
    app.mainloop()

(I used a lowercase letter this time, as the Python style guide reserves camel-case names for classes).

Your next problem is that Menu1.makeMenu() has no access to the app local variable in main_window(); you would need to pass that in:

menu = Menu1()
menu.makeMenu(app)

and

class Menu1:
    def makeMenu(self, app):
        app.config(menu=self)
        menu.add_cascade(label="Settings", menu=subMenu)
        subMenu.add_command(label="Settings", command=setting1)

Note that I changed menu to self there, menu was another local name in main_window.

The code still won't work because you haven't defined the name subMenu anywhere, but this is at least a step or 2 closer.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. I'll go back look a little more at my earlier testing files as well.
Is there anyway to make this work without removing the first class?
@Andreas: Sure, take a look at effbot.org/tkinterbook/tkinter-hello-again.htm on how to use classes properly. You are not really using classes correctly here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.