为什么立即调用我定义的函数?

时间:2016-05-10 19:09:34

标签: python function tkinter

    #Modules
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
import shutil
def stopProg(e):
    root.destroy()
def askfile(pathtype):
    filename = askopenfilename(parent=mainframe,title='Choose a file')
    pathtype.set(filename)
'''def askfilehome():
    filename = askopenfilename(parent=mainframe,title='Choose a file')
    homepath.set(filename)'''
def copyfileusb2pc():
    shutil.copyfile(homepath,r'H:\Documents\folder\backups')
    shutil.copy(usbpath,homepath)


#GUI
root = Tk()
root.title('PPSSPP Save Management')
mainframe = ttk.Frame(root, padding = "3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

usbpath = StringVar()
homepath = StringVar()
usbpath_entry = ttk.Entry(mainframe,width = 30,textvariable=usbpath)
usbpath_entry.grid(column=1, row=2, sticky=(W,E))
ttk.Label(mainframe, text="USB(PSP) Path").grid(column=1,row=1,sticky=W)
ttk.Button(mainframe, text="Browse", command=askfile(usbpath)).grid(column=3,row=2,sticky = E)
ttk.Label(mainframe, text="PC (PPSSPP) Path").grid(column=1,row=3,sticky=W)
homepath_entry = ttk.Entry(mainframe,width = 30,textvariable=homepath)
homepath_entry.grid(column=1,row=4, sticky=(W,E))
ttk.Button(mainframe, text="Browse", command=askfile(homepath)).grid(column=3, row =4,sticky=E)
ttk.Button(mainframe, text="USB -> PC", command=copyfileusb2pc).grid(column=1,row=5,sticky=(N,S,W,E))
ttk.Button(mainframe, text="PC -> USB").grid(column=1,row=6,sticky=(N,S,W,E))

当从IDLE运行这个tkinter程序时,我定义的函数名为" askfile"在我按下指定的按钮之前立即调用。为什么会这样,我该如何预防呢?

1 个答案:

答案 0 :(得分:0)

使命令= lambda x:function_name(params)而不是command = function_name。

通过指定,调用函数的askfile(homepath)而不是将函数作为参数传递。