Calling python function from MATLAB when using python Tkinter results in "TclError: Can't find a usable init.tcl in the following directories"

29 次查看(过去 30 天)
I am trying to call a python function in a script with Tkinter from MATLAB.
I am able to send data from python to MATLAB using the matlab engine, and I can call python functions in MATLAB when the python file does not use a Tkinter gui. I only run into the issue when I am trying to call a function in a file that has a gui.
The error output I recieve:
Error using __init__ (line 2023)
Python Error: TclError: Can't find a usable init.tcl in the following directories:
C:/Users/TS4804/AppData/Local/Programs/Python/Python37/lib/tcl8.6 {C:/Program Files/MATLAB/R2020a/bin/lib/tcl8.6} {C:/Program Files/MATLAB/R2020a/lib/tcl8.6} {C:/Program Files/MATLAB/R2020a/bin/library} {C:/Program Files/MATLAB/R2020a/library} {C:/Program Files/MATLAB/R2020a/tcl8.6.9/library} {C:/Program Files/MATLAB/tcl8.6.9/library}
This probably means that Tcl wasn't installed properly.
Error in test_gui><module> (line 22)
Error in <frozen importlib>_call_with_frames_removed (line 219)
Error in <frozen importlib>exec_module (line 728)
Error in <frozen importlib>_load_unlocked (line 677)
Error in <frozen importlib>_find_and_load_unlocked (line 967)
Error in <frozen importlib>_find_and_load (line 983)
Error in <frozen importlib>_gcd_import (line 1006)
Error in __init__>import_module (line 127)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\TS4804\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "test_gui.py", line 62, in send_to_matlab
print(eng.pythonInterface(1, list_to_send))
File "C:\Users\TS4804\AppData\Local\Programs\Python\Python37\lib\site-packages\matlab\engine\matlabengine.py", line 71, in __call__
_stderr, feval=True).result()
File "C:\Users\TS4804\AppData\Local\Programs\Python\Python37\lib\site-packages\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\TS4804\AppData\Local\Programs\Python\Python37\lib\site-packages\matlab\engine\fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError: Python Error: TclError: Can't find a usable init.tcl in the following directories:
C:/Users/TS4804/AppData/Local/Programs/Python/Python37/lib/tcl8.6 {C:/Program Files/MATLAB/R2020a/bin/lib/tcl8.6} {C:/Program Files/MATLAB/R2020a/lib/tcl8.6} {C:/Program Files/MATLAB/R2020a/bin/library} {C:/Program Files/MATLAB/R2020a/library} {C:/Program Files/MATLAB/R2020a/tcl8.6.9/library} {C:/Program Files/MATLAB/tcl8.6.9/library}
This probably means that Tcl wasn't installed properly.
As far as I can tell, because the GUI is running properly without MATLAB TCL is properly downloaded. It also comes built in for recent python installations. I have also tried to fix it by downloading the tcl files from activestate and added the paths of the files to matlab.
I am using:
MATLAB R2020a, python 3.7.7
Any help would be greatly appreciated

采纳的回答

Tom Sloan
Tom Sloan 2020-5-19
Figured it out, I had downloaded the wrong tcl library x.x.10 instead of x.x.9

更多回答(1 个)

choi
choi 2021-4-21
Hi.
Here is a example
======
pyExec = 'C:\Users\djchoi\AppData\Local\Programs\Python\Python37\pythonw.exe';
pyversion(pyExec);
pyStr = pyenv;
pyTCL = fullfile(pyStr.Home, 'tcl', 'tcl8.6');
pyTK = fullfile(pyStr.Home, 'tcl', 'tk8.6');
setenv('TCL_LIBRARY', pyTCL);
setenv('TK_LIBRARY', pyTK);
win = py.tkinter.Tk();
win.title('My GUI');
win.mainloop();
clear win

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by