2016-05-10 37 views
-2
#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」的函數會在我按下它分配給的按鈕之前立即調用。爲什麼是這樣的,我如何防止它?爲什麼立即調用我定義的函數?

回答

0

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

通過指定askfile(homepath),您正在調用該函數,而不是將該函數作爲參數傳遞。

相關問題