N
N
Nikolai2021-12-12 19:50:03
Python
Nikolai, 2021-12-12 19:50:03

Why is the app object not being defined when I referenced it in the Menu class?

I can’t understand why there are errors in the code on lines 12 and 35. It seems to me that on the 6th it’s not an error, but a consequence of other actions that I performed later. There are only 2 lines left. And I think it's 35, but what's wrong?

[email protected]:~/work$ python3 main.py
Traceback (most recent call last):
​ File "main.py", line 35, in
​ ​ ​ app = App()
​ File "main.py", line 6 , in __init__
​ ​ ​ self.initialization() #
​ File "main.py", line 12, in initialization
​ ​ ​ pop_menu = tk.Menu(app) NameError
: name 'app' is not defined

:

import tkinter as tk
​class
App(tk.Tk):
​ ​ ​ ​ def __init__(self):
​ ​ ​ ​ ​ ​ ​ super().__init__()
​ ​ ​ ​ ​ ​ self.initialization() #
​​ ​ ​ ​ def
initialization(self):
​ ​ ​ ​ ​ ​ ​ btn1 = tk.Button(text="Finish", command=self.btn_exit
)​ ​ ​ ​ ​ btn1.pack(padx=20, pady=20)
​​ ​ ​ ​ ​ ​ ​ pop_menu
= tk.Menu(app)
​ ​ ​ ​ ​ ​ ​ app.config(menu=pop_menu
)
file_menu = tk.Menu(pop_menu, tearoff=
0
) add_command(label="Start testing\tCtrl+T")
​ ​ ​ ​ ​ ​ ​ file_menu.add_command(label="Save logs\tCtrl+S")
​ ​ ​ ​ ​ ​ ​ help_menu.add_command(label="Exit ")
​​ ​ ​ ​ ​ ​ help_menu
= tk.Menu(pop_menu, tearoff=0)
​ ​ ​ ​ ​ ​ help_menu.add_command(label="Documentation")
​ ​ ​ ​ ​ ​ ​ help_menu.add_command(label="Changelogs")
​ ​ ​ ​ ​ ​ ​ help_menu.add_command(label="Credits" )
​​ ​ ​ ​ ​ ​ pop_menu.add_cascade
(label="File", menu=file_menu)
​ ​ ​ ​ ​ ​ ​ pop_menu.add_cascade(label="Help", menu=help_menu )
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ quit() ​​if __name__ == "__main__": ​ ​ ​ ​ app = App() ​ ​ ​ ​ app["bg"] = "pink "​ ​ #app.iconbitmap("@/home/cerberus/work/dagger.xbm") ​ ​ ​ version = 0.6 ​ ​ ​ app.title("Vine crasher v{}".format(version)) ​​ ​ ​ w = app.winfo_screenmmwidth()
​ ​ ​ h = app.winfo_screenmmheight()
​ ​ ​ w = w // 2
​ ​ ​ h = h // 2
​ ​ ​ app.geometry("600x400+{}+{}".format(w, h) )
​ ​ ​ app.resizable(False, False)

​ ​ ​ app.mainloop()

I'm sure the solution is simple, I just don't see it
.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-12-12
@NiklausVarm

pop_menu = tk.Menu(self)
You should read about scopes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question