我想在tcl
计划中拨打proc
python
。
tcl
脚本以
proc scale_wigner-seitz_radii { } {
(我不确定我是否可以在此处填写完整的proc,因为这是许可程序的一部分。)
这个程序由我的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代码,请详细说明。
答案 0 :(得分:2)
问题似乎是这一行:
r = Tkinter.Tk
我的猜测是,您认为这是创建Tk
的实例,但您只是保存对类的引用,而不是创建实例班级。实例化它时,返回的对象具有名为tk
的属性,该对象在内部用于引用tcl解释器。由于您没有实例化它,r
(指向Tk
)没有这样的属性。
要修复它,请通过添加括号来实例化该类:
r = Tkinter.Tk()
r
现在将成为Tk
对象的正确引用,并且应该具有tk
属性,您可以在原始tcl代码上调用eval
。