TransWikia.com

How to control gnome-terminal from Python scrypt?

Ask Ubuntu Asked by Anthony Scaife on December 10, 2021

I am developing an application in PyGtk, and would like to launch a gnome-terminal and output commands to it.

My user should then be able to modify the command, or maybe ignore using the up arrow … etc.

I have been able to launch a terminal, but can’t work out how to send commands.

This is how my application starts:

class App(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self)

        process=subprocess.Popen(["gnome-terminal", "--class=App", "--name=app"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        response,error=process.communicate()

2 Answers

I had the same problem.

Solved it with tmux, thanks to this answer (copied below).

In the terminal that should receive the command start tmux with an identifier:

tmux new-session -s MYSES

Send commands to it with:

tmux send-keys -t MYSES "ls -l"$'n'

Answered by naroom on December 10, 2021

According to the python docs, you want to use Popen.communicate(input=None) http://docs.python.org/2/library/subprocess.html#popen-objects

I would recommend that you edit your command from python before sending it to another process. For example, display a window with the default command and allow the user to edit (or cancel) it before executing it. Also, "gnome-terminal" is probably overkill for this, "/bin/bash" should be sufficient.

If that doesn't work for you, you could also try this. (depending on what you are actually running from the terminal) Since you mentioned users could edit the command, it would be a good idea to validate the input before running.

command = ['ls','-l']
output = subprocess.check_output( command )
print( output )

Answered by David on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP