Article
· Feb 17, 2022 2m read

Embedded Python and tcl tkinter on Windows

If your embedded python code calls tkinter library (which is used by a lot of graphic producing libraries, including matplotlib), you might get this error:

<THROW> *%Exception.PythonException <CLASS DOES NOT EXIST> 230 ^^0^DO ##CLASS(User.Test).Test() 
<class '_tkinter.TclError'>: Can't find a usable init.tcl in the following directories:

c:/intersystems/irispy/lib/python/lib/tcl8.6
c:/intersystems/irispy/lib/tcl8.6
c:/intersystems/lib/tcl8.6
c:/intersystems/irispy/library
c:/intersystems/library
c:/intersystems/tcl8.6.9/library
c:/tcl8.6.9/library

This probably means that Tcl wasn't installed properly.

Here's a code sample to trigger this error:

Class User.Test
{

/// do ##class(User.Test).Test()
ClassMethod Test() [ Language = python ]
{
import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)')
ax.grid()

fig.savefig("test.png")
}

}

In that case you need no install tcl and tk libraries. One of the approaches is building from source.

For windows you can get prebuilt binaries here. For example, based on my error I need version-8.6.12.5. Download it and from the lib folder copy tcl8.6 and tk8.6 to c:/intersystems/irispy/lib/python/lib/ or any other path from your error message.

After that you may get another error:

Can't find a usable init.tcl in the following directories:
c:/intersystems/irispy/lib/python/lib/tcl8.6 
c:/intersystems/irispy/lib/tcl8.6 
c:/intersystems/lib/tcl8.6 
c:/intersystems/irispy/library 
c:/intersystems/library 
c:/intersystems/tcl8.6.9/library 
c:/tcl8.6.9/library

c:/intersystems/irispy/lib/python/lib/tcl8.6/init.tcl: version conflict for package "Tcl": have 8.6.9, need exactly 8.6.12
version conflict for package "Tcl": have 8.6.9, need exactly 8.6.12
while executing "package require -exact Tcl 8.6.12"
(file "c:/intersystems/irispy/lib/python/lib/tcl8.6/init.tcl" line 19)
invoked from within "source c:/intersystems/irispy/lib/python/lib/tcl8.6/init.tcl" ("uplevel" body line 1)
invoked from within "uplevel #0 [list source $tclfile]"

This probably means that Tcl wasn't installed properly.

That means binaries are from a different version then what might be expected, but as it's a micro difference it can be solved by opening init.tcl and replacing

package require -exact Tcl 8.6.12

with

package require -exact Tcl 8.6.9

Do the same in tk.tcl:

package require -exact Tk  8.6.12

with

package require -exact Tk  8.6.9

And after that tkinter should work.

Discussion (0)1
Log in or sign up to continue