Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my python tkinter button is executed automatically

Tags:

python

tkinter

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!

like image 845
fyr91 Avatar asked Sep 17 '26 11:09

fyr91


1 Answers

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

..., command=(lambda: exp(Output)))
like image 165
Ignacio Vazquez-Abrams Avatar answered Sep 18 '26 23:09

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!