1

I am writing a python program that uses Tkinter for UI. I have to separate python files: interface.py and script.py. The script.py contains a code which calls a matlab script using matlab.engine module. The interface.py contains UI elements should call script.py when I press a certain button i.e. after I press a button in my program it runs matlab.scripts specified in script.py. However when I run interface.py, even though I just import script.py without actually calling its functions, it still starts matlab.engine on the program startup, as I understand it compiles and interpetes script.py when I import it. What I need is that matlab.engine written inside a function in script.py will be called when I want it to and not during the program startup.

Here is script.py:

import matlab.engine

def implement():

  eng = matlab.engine.start_matlab()
  eng.matlab_script(nargout=0)
  eng.quit()

Here is interface.py snippet containing the function and the button:

def analyze():
        import script
        script.implement()
        
analyzeButton = Button(self, text = "Analyze ->", bg='#0D0628', width=10, height=2, command = analyze())
0

1 Answer 1

0

When you execute this line of code:

analyzeButton = Button(self, text = "Analyze ->", bg='#0D0628', 
                       width=10, height=2, command = analyze())

you are actually running the analyze function, which will start the MATLAB engine and run your MATLAB script.

In Python, analyze is a reference to a function, and analyze() calls the function. You want to set command=analyze, not command=analyze(). The former will pass the function reference to the button constructor, the latter will run the function and pass the function's return value to the button constructor.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.