Reputation: 131
Hello I have some very simple python code:
from Tkinter import *
from tkFileDialog import askopenfilename
def createnewguiprojectgui():
asktk = Toplevel()
asktk.title("Create new gui source")
Label(asktk, text="Gui options").pack(side=TOP)
askfilename = Entry(asktk)
askfilename.insert(0, "Source name")
askfilename.pack(side=LEFT,fill=X)
yesfornew = Button(asktk, text="cancel", command=createnewguiproject())
nofornew = Button(asktk, text="cancel",command=) #a command
def createnewguiproject():
pass
def main():
mainparent = Tk()
w, h = mainparent.winfo_screenwidth(), mainparent.winfo_screenheight()
mainparent.title("GUI CREATOR")
mainmenu = Menu(mainparent)
filemenu = Menu(mainmenu,tearoff = 0)
filemenu.add_command(label="New project", command = createnewguiprojectgui())
mainmenu.add_cascade(label="File", menu=filemenu)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)
mainparent.config(menu=mainmenu)
mainmenu.focus_set()
mainparent.mainloop()
if __name__ == "__main__":
main()
However, every time I run ths script, the asktk toplevel pops up with the mainparent Tk, even if I don't press the menu bar, and the focus is set to asktk. Whats wrong?
Upvotes: 0
Views: 327
Reputation: 2689
Don't call the function command=createnewguiproject()
. Remove the (). Your createnewguiproject()
method gets called as soon as the main loop starts.
Do this:
yesfornew = Button(asktk, text="cancel", command=createnewguiproject)
Or if you want to send some argument then:
yesfornew = Button(asktk, text="cancel", command=lambda:createnewguiproject(args))
Upvotes: 1
Reputation: 36
The command here is calling the function that creates a new window object onload rather than when New Project is selected.
filemenu.add_command(label="New project", command = createnewguiprojectgui())
You need to change it to:
filemenu.add_command(label="New project", command = createnewguiprojectgui)
Upvotes: 2
Reputation: 3158
Try this: in the line that has ...
filemenu.add_command(label="New project", command = createnewguiprojectgui())
take the () off the end of createnewguiprojectgui. In other words, pass the function, don't call it.
The same should be true for the following line ...
yesfornew = Button(asktk, text="cancel", command=createnewguiproject())
Upvotes: 2