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:
# Open the window root = tk.Tk() root.title("Newton") root.geometry("400x500") root.resizable(width=False, height=False) # Chat window menu settings main_menu=Menu(root) file_menu=Menu(root) # Add commands to submenu file_menu.add_command(label="New..") file_menu.add_command(label="Save As..") file_menu.add_command(label="Exit") main_menu.add_cascade(label="File", Menu=file_menu) # Add the rest of the menu options to the main menu main_menu.add_command(label="Edit") main_menu.add_command(label="Quit") root.config(Menu=main_menu)
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:
# Chat window menu settings main_menu = tk.Menu(root) # Just like root = tk.Tk() file_menu = tk.Menu(root)
Also one more thing I noticed:
root.config(menu=main_menu) # Instead of Menu=main_menu