3

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!

1 Answer 1

5

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

..., command=(lambda: exp(Output)))
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Ignacio, this is very helpful. I have applied this to other button, they are running perfectly except one. I got a button to exit the frame. when i add this: command = (lambda: root.destroy), it is not working, may I know why?
okay, thank you, i have change the root.destroy to des(root) function. it is working now. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.