Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run commands on same TCL shell using Python

Tags:

python

tcl

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.

like image 655
Sumit Avatar asked Mar 03 '16 09:03

Sumit


People also ask

How do you call a Tcl script in Python?

As Tcl is a string-interpreted language where everything is a string, evaluating Tcl calls within Python is effectively the same as evaluating a string object containing Tcl code using the interpreter's eval() function.

How do you execute a command in Tcl?

The exec command runs programs from your Tcl script. For example: Unlike other UNIX shell exec commands, the Tcl exec does not replace the current process with the new one. Instead, the Tcl library forks first and executes the program as a child process.

How do you pass command line arguments in Tcl?

The Tcl shells pass the command-line arguments to the script as the value of the argv variable. The number of command-line arguments is given by the argc variable. The name of the program, or script, is not part of argv nor is it counted by argc. Instead, it is put into the argv0 variable. ...

How do you call a proc in Tcl?

To create a TCL procedure without any parameter you should use the proc keyword followed by the procedure name then the scope of your procedure. proc hello_world {} { // Use puts to print your output in the terminal. // If your procedure return data use return keyword. } Save this answer.


2 Answers

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 :-)

like image 157
Colin Macleod Avatar answered Sep 29 '22 20:09

Colin Macleod


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 [email protected]:.')
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.

like image 33
Sharad Avatar answered Sep 29 '22 20:09

Sharad