0

Ok, so I have this project in python 3x where I'm using Tkinter to make a Notepad, but the functions of the Menu Bar are initializing without you click there. I made an example where I put all the functions to print '1', and when I run the code, instantly it prints a lot of 1's. Idk what to do pls help me T-T.

This is the code:

root = Tk()
root.title(' "Untitled" - Notepad')
root.iconbitmap("E:/Projects/Notepad/notepadicon.ico")
twidth = 600
theight = 400
root.geometry('{}x{}'.format(twidth, height))
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)


textArea = Text(root)
menuBar = Menu(root)
fileMenu = Menu(menuBar, tearoff=0)
editMenu = Menu(menuBar, tearoff=0)
helpMenu = Menu(menuBar, tearoff=0)
scrollBar = Scrollbar(textArea)

def menu ():

    textArea.grid(sticky=N + E + S + W)
    fileMenu.add_command(label="New", command=newFile())
    fileMenu.add_command(label="Open", command=openFile())
    fileMenu.add_command(label="Save", command=saveFile())
    fileMenu.add_separator()
    fileMenu.add_command(label="Exit", command=exitApp())
    menuBar.add_cascade(label="File", menu=fileMenu)

    editMenu.add_command(label="Cut", command=cut())
    editMenu.add_command(label="Copy", command=copy())
    editMenu.add_command(label="Paste", command=paste())
    menuBar.add_cascade(label="Edit", menu=editMenu)

    helpMenu.add_command(label="About", command=openAbout())
    menuBar.add_cascade(label="Help", menu=helpMenu)

    root.config(menu=menuBar)

    scrollBar.pack(side=RIGHT, fill=Y)
    scrollBar.config(command=textArea.yview)
    textArea.config(yscrollcommand=scrollBar.set)

def newFile():
    print(1)
def openFile():
    print(1)
def saveFile():
    print(1)
def exitApp():
    print(1)

def cut():
    print(1)
def copy():
    print(1)
def paste():
    print(1)
def openAbout():
    print(1)

menu()
root.mainloop()

The UI

The Run result

Moises
  • 1
  • Take the `()` off of each command setting, you want to assign the function, not the value of the function. Example: `fileMenu.add_command(label="New", command=newFile())` should be `fileMenu.add_command(label="New", command=newFile)` – tgikal Apr 13 '20 at 20:53
  • thanks, bro, it worked and im really happy – Moises Apr 13 '20 at 21:37

0 Answers0