【发布时间】:2015-09-07 20:16:41
【问题描述】:
我正在尝试在 python 程序中调用 tcl proc。
tcl 脚本以
proc scale_wigner-seitz_radii { } {
(我不确定是否可以将完整的过程放在这里,因为这是许可程序的一部分)。
这个程序是由我的python脚本调用的:
#!/usr/bin/python3
import sys
from numpy import arange
from tempfile import mkstemp
from shutil import move, copy
from os import remove, close, mkdir, path
import Tkinter
def repll(file_path, pattern, subst):
print(pattern)
print(subst)
print(file_path)
r = Tkinter.Tk
# fullpath = str(subst) + "/" + file_path
fh, abs_path = mkstemp()
with open(abs_path, "w") as new_file:
with open(file_path, "r") as old_file:
for line in old_file:
new_file.write(line.replace(pattern.strip(),
str(subst).strip()))
r.tk.eval('source /home/rudra/WORK/xband/create_system_util.tcl')
r.tk.eval('proc scale_wigner-seitz_radii')
copy(abs_path, path.join(str(subst), file_path))
inpf = str(sys.argv[1])
a = []
print (inpf)
with open(inpf, "r") as ifile:
for line in ifile:
if line.startswith("lattice parameter A"):
a = next(ifile, "")
print(a)
for i in arange(float(a)-.10, float(a)+.10, 0.02):
if not path.exists(str(i)):
mkdir(str(i))
repll(inpf, a, i)
我没有做一个最小的例子,因为这似乎比用英语解释更好。
在def repll 的最后,它调用tcl proc。之前没遇到过tcl脚本,从this question找到调用进程。
但是当我运行它时,我得到了错误:
Traceback (most recent call last):
File "xband.py", line 41, in <module>
repll(inpf, a, i)
File "xband.py", line 24, in repll
r.tk.eval('source /home/rudra/WORK/xband/create_system_util.tcl')
AttributeError: class Tk has no attribute 'tk'
我该如何解决这个问题?
Donal 发表评论后 感谢您的回复。听从您的建议后,我从source 行收到了同样的错误。
Traceback (most recent call last):
File "xband.py", line 41, in <module>
repll(inpf, a, i)
File "xband.py", line 24, in repll
r.tk.eval('/home/rudra/WORK/xband/create_system_util.tcl')
AttributeError: class Tk has no attribute 'tk'
对不起,如果它很傻,但是由于 tcl 在不同的文件中,我必须首先获取它,对吗?而且,正如我所说,这是我正在查看的第一个 tcl 代码,请详细说明。
【问题讨论】:
-
你还没有遇到这个错误,但是
r.tk.eval('proc scale_wigner-seitz_radii')是错误的。 Tclproc命令定义命令;要调用它们,您只需使用命令名称:r.tk.eval('scale_wigner-seitz_radii')。您还需要整理出您的其他错误(修复可能也适用于此处)。 -
从 Tcl 的角度来看,您必须首先
source文件,然后调用其中的过程。这可以在单个脚本调用中完成:source /home/rudra/WORK/xband/create_system_util.tcl; scale_wigner-seitz_radii(使用;分隔两个命令调用)。您还需要理清 Python 方面的问题,您似乎在错误的实体上调用tk方法。是否需要实例化Tk类? -
您好,Donal,如果您有空闲时间,我可以联系您吗?正如我所说,由于 tcl 是许可代码的一部分,我不太怀疑将其公开
-
我真的对 Python 的了解还不够,无法为您提供适当的帮助。我已经提供了您真正需要的所有 Tcl 帮助(好吧,无论如何,这个特殊问题)。而且我在一周中往往有相当忙碌的日子......
标签: python-3.x tcl