When clicking an option in the menu bar, a new window is suppose to appear when clicked. However, the new window is appearing immediately when main program begins to run, before clicking an option in the menu.
How can the window appear only when option is clicked and not immediately when the main program begins to run?
#Main Program
from tkinter import *
from tkinter import ttk
import module
root = Tk()
main_menu_bar = Menu(root)
main_option = Menu(main_menu_bar, tearoff=0)
main_option.add_command(label = "Option 1", command = module.function())
main_menu_bar.add_cascade(label="Main Option", menu=main_option)
root.config(menu=main_menu_bar)
root.mainloop()
#Module
from tkinter import *
from tkinter import ttk
def function ():
new_window = Toplevel()
Instead of:
Try:
If you put the parentheses, the function will be executed immediately, while if you don't put them it will only be a reference to this function that will be executed upon the event signal.
To make it clearer, the same thing happens if you want to store functions in a list for later execution: