如何使用 Python 在同一 TCL shell 上 运行 命令
How to run commands on same TCL shell using Python
我所有的库都是用 TCL 编写的。我想在 Python 中创建一个 GUI,它将有几个按钮和其他选项。在开始时TCL shell 会打开。当我点击按钮时,相应的命令将在 TCL shell.
上执行
是否可以在不关闭 TCL shell.
的情况下在同一 shell TCL 上触发命令
我搜索了 google 并在 Python 中找到了 Tkniter
模块,但每次我需要执行命令时它都会打开 TCL shell。
这个问题可以用Pexpect
来解决
Pexpect is a Python module for spawning child applications and controlling
them automatically. Pexpect can be used for automating interactive applications
such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
scripts for duplicating software package installations on different servers. It
can be used for automated software testing. Pexpect is in the spirit of Don
Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
require TCL and Expect or require C extensions to be compiled. Pexpect does not
use C, Expect, or TCL extensions. It should work on any platform that supports
the standard Python pty module. The Pexpect interface focuses on ease of use so
that simple tasks are easy.
使用示例直接取自 Pexpect 网站
child = pexpect.spawn('scp foo myname@host.example.com:.')
child.expect ('Password:')
child.sendline (mypassword)
您可以将终端生成为子进程,然后在 GUI 生成事件时使用该子进程发送命令。
您当然可以使用 Tkinter 运行 在同一个 Tcl 解释器中执行一系列命令:
Python 2.7.9 (default, Feb 28 2016, 05:52:45) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.tk.eval('set msg "hello world"')
'hello world'
>>> root.tk.eval('string length $msg')
'11'
>>> root.tk.eval('foreach x {1 2 4} {puts "$msg $x"}')
hello world 1
hello world 2
hello world 4
''
>>>
- 这里变量 msg 在一个命令中设置,它的值在后面的命令中使用,如果我们为每个命令创建一个新的解释器,这将不起作用。
如果您不想要创建的 Tk window,只需 运行 root.tk.eval('wm withdraw .')
将其隐藏。
如果这不能回答您的问题,您最好解释一下您还需要什么:-)
我创建了这个简单的 tcl 程序pgm.tcl
puts "Hello world"
我可以在控制台中启动它
tclsh pgm.tcl
以下是如何在 python
中启动它
from subprocess import Popen, PIPE
p1 = Popen( ['tclsh', 'pgm.tcl'], stdout=PIPE )
p1out, p1err = p1.communicate()
if p1out is not None: print (p1out)
if p1err is not None: print (p1err)
此答案 OS 依赖 (linux),但您应该能够将其适应其他 OS。
我所有的库都是用 TCL 编写的。我想在 Python 中创建一个 GUI,它将有几个按钮和其他选项。在开始时TCL shell 会打开。当我点击按钮时,相应的命令将在 TCL shell.
上执行是否可以在不关闭 TCL shell.
的情况下在同一 shell TCL 上触发命令我搜索了 google 并在 Python 中找到了 Tkniter
模块,但每次我需要执行命令时它都会打开 TCL shell。
这个问题可以用Pexpect
来解决Pexpect is a Python module for spawning child applications and controlling them automatically. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python require TCL and Expect or require C extensions to be compiled. Pexpect does not use C, Expect, or TCL extensions. It should work on any platform that supports the standard Python pty module. The Pexpect interface focuses on ease of use so that simple tasks are easy.
使用示例直接取自 Pexpect 网站
child = pexpect.spawn('scp foo myname@host.example.com:.')
child.expect ('Password:')
child.sendline (mypassword)
您可以将终端生成为子进程,然后在 GUI 生成事件时使用该子进程发送命令。
您当然可以使用 Tkinter 运行 在同一个 Tcl 解释器中执行一系列命令:
Python 2.7.9 (default, Feb 28 2016, 05:52:45) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.tk.eval('set msg "hello world"')
'hello world'
>>> root.tk.eval('string length $msg')
'11'
>>> root.tk.eval('foreach x {1 2 4} {puts "$msg $x"}')
hello world 1
hello world 2
hello world 4
''
>>>
- 这里变量 msg 在一个命令中设置,它的值在后面的命令中使用,如果我们为每个命令创建一个新的解释器,这将不起作用。
如果您不想要创建的 Tk window,只需 运行 root.tk.eval('wm withdraw .')
将其隐藏。
如果这不能回答您的问题,您最好解释一下您还需要什么:-)
我创建了这个简单的 tcl 程序pgm.tcl
puts "Hello world"
我可以在控制台中启动它
tclsh pgm.tcl
以下是如何在 python
中启动它from subprocess import Popen, PIPE
p1 = Popen( ['tclsh', 'pgm.tcl'], stdout=PIPE )
p1out, p1err = p1.communicate()
if p1out is not None: print (p1out)
if p1err is not None: print (p1err)
此答案 OS 依赖 (linux),但您应该能够将其适应其他 OS。