-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))

When running this tkinter program from IDLE, the function I have defined called "askfile" is called instantly before I press the button that it is assigned to. Why is this and how do I prevent it?

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
SolidSnackDrive
  • 213
  • 2
  • 10

1 Answers1

0

Make your command = lambda x: function_name(params) instead of command = function_name.

By specifying, askfile(homepath) you are calling the function rather than passing the function in as a parameter.

mattsap
  • 3,790
  • 1
  • 15
  • 36