DeeWBee
DeeWBee

Reputation: 695

How to stop Python from executing script on import?

I have a Tkinter-based GUI with a series of buttons. I want one of these buttons to execute a command from another script - testExecute.py - when pressed (code for both scripts included below).

Right now, when I start the GUI, the external script function appears to be executing on import rather than when I press the button (pressing the button doesn't appear to execute the function either). I did some research and included the if __name__ == "__main__": bit in testExecute.py, but it still executes on import in my main script. Any thoughts?

EXTRA QUESTION IN RESPONSE TO ANSWERS BELOW: What do I do if I want to pass an argument to the function? Because if I include the argument, the function again executes on import. But if I don't include the argument, I get an error when I press the button.

Main Script:

from tkinter import *
from tkinter.ttk import *
import testExecute as testEx

class mainGUI(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent

        self.initUI()


    def initUI(self):

        self.parent.title("GUIV0.1")
        self.pack(fill=BOTH, expand=True)

        self.columnconfigure(1, weight = 1)
        self.columnconfigure(3, pad = 7)
        self.rowconfigure(3, weight = 1)
        self.rowconfigure(5, pad = 7)

        lbl = Label(self, text = "Windows")
        lbl.grid(sticky = W, pady=4, padx=5)

        area = Text(self)
        area.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)

        abtn = Button(self, text="Activate", command = testEx.testFunc())
        abtn.grid(row=1, column=3)

        cbtn = Button(self, text="Close")
        cbtn.grid(row=2, column=3, pady=4)

        hbtn = Button(self, text="Help")
        hbtn.grid(row=5, column=0, padx=5)

        obtn = Button(self, text="OK")
        obtn.grid(row=5, column=3)


def main():

    root = Tk()
    app = mainGUI(root)
    root.mainloop()

if __name__ == '__main__':
    main() 

testExecute.py:

def testFunc():
    print("Test test test")
    print("I do nothing, if you see this text, I am hiding in your code!")

if __name__ == "__main__":
    testFunc()

Upvotes: 0

Views: 110

Answers (2)

wilsojb
wilsojb

Reputation: 41

Check out http://effbot.org/tkinterbook/button.htm.

The problem is that you are calling the command callback during initialization. Change,

abtn = Button(self, text="Activate", command = testEx.testFunc())

to

abtn = Button(self, text="Activate", command = testEx.testFunc)

and you should be all good. (Note the lack of parenthesis following testFunc).

Upvotes: 3

syntonym
syntonym

Reputation: 7384

When you create the button you are executing the function directly. Instead you should bind to the function itself. So

   abtn = Button(self, text="Activate", command = testEx.testFunc())

should be

   abtn = Button(self, text="Activate", command = testEx.testFunc)

Upvotes: 4

Related Questions