Kris Rice
Kris Rice

Reputation: 923

Python: button command isn't running?

For an A-level computing project, I am making a car data monitoring system. I have a button that opens the filedialog.askopenfilename method. When I pass this through a method like below, it doesn't work. However when I pass it straight into the button, it works fine. Any ideas as to why?

Doesn't work:

def get_data_file():
    filedialog.askopenfilename
    return
OpenfileButton=Button(master,text="Select File",width=20,command=get_data_file).grid(row=3, column=2)

works:

OpenfileButton=Button(master,text="Select File",width=20,command=filedialog.askopenfilename).grid(row=3, column=2)

Upvotes: 0

Views: 52

Answers (1)

Paul Rooney
Paul Rooney

Reputation: 21609

You need to actually call the function

def get_data_file():
    filedialog.askopenfilename()

When you pass the function to the button you should not call it but simply pass it to be called when the button is clicked, but as you have now wrapped it in another function it must be called by you.

The return is redundant and can be left out if you wish. All python functions return None by default.

Upvotes: 4

Related Questions