So i’m creating a chat bot inside of a tkinter gui but keep getting an error message when creating the main menu and sub menu. Any help is appreciated. Here’s the code:
JavaScript
x
21
21
1
# Open the window
2
root = tk.Tk()
3
root.title("Newton")
4
root.geometry("400x500")
5
root.resizable(width=False, height=False)
6
7
# Chat window menu settings
8
main_menu=Menu(root)
9
file_menu=Menu(root)
10
11
# Add commands to submenu
12
file_menu.add_command(label="New..")
13
file_menu.add_command(label="Save As..")
14
file_menu.add_command(label="Exit")
15
main_menu.add_cascade(label="File", Menu=file_menu)
16
17
# Add the rest of the menu options to the main menu
18
main_menu.add_command(label="Edit")
19
main_menu.add_command(label="Quit")
20
root.config(Menu=main_menu)
21
The error I’m getting is: main_menu=Menu(root) NameError:’Menu’ is not defined
Advertisement
Answer
Your import is import tkinter as tk
. So you need to suffix every item imported from the module with tk
, like:
JavaScript
1
4
1
# Chat window menu settings
2
main_menu = tk.Menu(root) # Just like root = tk.Tk()
3
file_menu = tk.Menu(root)
4
Also one more thing I noticed:
JavaScript
1
2
1
root.config(menu=main_menu) # Instead of Menu=main_menu
2