简体   繁体   中英

How to run commands on same TCL shell using Python

I am having all the libraries written in TCL. I want to create a GUI in Python which will have few buttons and other options. In the start TCL shell will open. When I will click the buttons, respective commands will be executed on the TCL shell.

Is it possible to fire commands on the same shell of TCL without closing TCL shell.

I searched google and find Tkniter module in Python but it will open TCL shell everytime I need to execute command.

You can certainly use Tkinter to run a series of commands in the same Tcl interpreter:

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

- here the variable msg is set in one command and its value is used in later commands, which would not work if we were creating a new interpreter for each command. If you don't want the Tk window that gets created, just run root.tk.eval('wm withdraw .') to hide it.

If this doesn't answer your question you had better explain what else it is that you need :-)

This problem can be solved by using 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.

Usage example taken directly from the Pexpect website

child = pexpect.spawn('scp foo myname@host.example.com:.')
child.expect ('Password:')
child.sendline (mypassword)

you can spawn the terminal as a child process and then use this child to send commands when GUI generates an event.

I created this simple tcl program pgm.tcl

puts "Hello world"

I can launch it in a console

tclsh pgm.tcl

Here is how it can be launched in 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)

This answer is OS dependent (linux), but you should be able to adapt it to other OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM