文章
· 三月 2, 2022 阅读大约需 2 分钟

Windows上内嵌式Python调用tcl tkinter时的一个报错及处理方法

如果你的嵌入式Python代码调用了 tkinter library库(它被很多图形制作库使用,包括matplotlib),你可能会得到这个错误:

<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.

下面是一个触发这个错误的代码样本:

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")
}

}

在这种情况下,你不需要安装tcl和tk库。其中一个方法是从源代码开始构建。In that case you need no install tcl and tk libraries. One of the approaches is 从源代码来构建

对于windows系统,你可以在这里获得预构建的二进制文件。例如,根据我的错误,我需要8.6.12.5版本。下载它并从lib文件夹中复制tcl8.6和tk8.6到 c:/intersystems/irispy/lib/python/lib/ 或你的错误信息中的任何其他路径。

之后,你可能会得到另一个错误:

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.

这意味着二进制文件与预期的版本不同,但这是一个微小的差异,可以通过打开 init.tcl 并把

package require -exact Tcl 8.6.12

替换成

package require -exact Tcl 8.6.9

同样的,在 tk.tcl

package require -exact Tk  8.6.12

替换成

package require -exact Tk  8.6.9

之后,tkinter 就应该正常工作了。

讨论 (0)0
登录或注册以继续