fyr91
fyr91

Reputation: 1293

Why my python tkinter button is executed automatically

I have a python file which is called from other files Every time the python file is imported and mainApp is called from others, the tkinter button inside the python file is executed automatically. here is part of the python file code

from Tktable import *
def exp(Output):
    import csv
    from tkFileDialog import askdirectory
    folder=askdirectory();
    if folder:
        path = folder+'/outputTable.csv';
        file = open(path, 'w');
        writer = csv.writer(file)
        title = ['Premise','Conclusion','Support','Confidence','Lift']
        writer.writerow(title);
        zip(*Output)
        for item in zip(*Output):
            writer.writerow(item)
        file.close()
def mainApp(Output):
    from Tkinter import Tk, Label, Button, Scrollbar, Frame
    root = Tk()
    top = Frame(root).pack(side = 'top', fill = 'x')
    ...
    export = Button(top, text='EXPORT', command=exp(Output))
    export.grid(row=0, column=4, sticky = 'e')
    ...

How could I stop the auto execution of the button? And why is this happening? Can anyone help me? Thank you!

Upvotes: 3

Views: 4866

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799250

It happens because you're calling the function. Pass it a function object instead, such as one created with lambda.

..., command=(lambda: exp(Output)))

Upvotes: 4

Related Questions