1

I need to run a TCL script in python, my TCL script also has a user-defined(internal) package I tried these scripts:

1.

import Tkinter
import socket

def TCLRun():
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(('127.0.0.1', 5006))
 root = Tkinter.Tk()
## root.tk.eval('package require XXX')
 tcl_script ="""
package require XXX
set YYY [COMMAND FROM PACKAGE]
puts $YYY 
} """
 # call the Tkinter tcl interpreter
 root.tk.call('eval', tcl_script)
 root.mainloop()

with this error:

import TCLCall
>>> TCLCall.TCLRun()

    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        TCLCall.TCLRun()
      File "C:\Users\XXX\Desktop\PKT\TCLCall.py", line 24, in TCLRun
        root.tk.call('eval', tcl_script)
    TclError: can not find channel named "stdout"

and,

2.

import Tkinter
root=Tkinter.Tk()
root.tk.eval('package require XXX')
root.tk.eval('set YYY COMMAND')

returns error about sdtout!

other one:

3.

 import subprocess
    p = subprocess.Popen(
        "tclsh tcltest.tcl",
        shell=True,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    print stdout
    print stderr

returns the following error:

can't find package __teapot__

    while executing

"package require __teapot__"

none of them working, please help me with this issue!

I can use some commands that operating something on our product with following code:

import socket
import time

def Sckt():
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(('127.0.0.1', 5006))

 s.send('0 COMMAND \r\n')

that would give you some ideas!

thank,

4

1 回答 1

1

对于不属于您的供应商安装的自定义包,您需要指示 Tcl 从何处搜索它。为此,将包含包的目录的目录附加到 Tcl 的全局auto_path变量中。例如,如果 XXX 包在目录中,/home/amir/.tclpackages/XXX1.0那么您将让您的脚本执行以下操作:

lappend auto_path /home/amir/.tclpackages

这可以在package require XXX. (好吧,你可以把它放在之后,但然后package require会失败......)

于 2013-01-16T09:30:38.040 回答