使用参数启动 gnome-terminal

发布于 2024-10-10 16:10:48 字数 561 浏览 4 评论 0原文

使用 Python ,我想在新的终端窗口中启动一个进程,因为为了向用户展示正在发生的事情,并且涉及多个进程。

我尝试这样做:

>>> import subprocess
>>> subprocess.Popen(['gnome-terminal'])
<subprocess.Popen object at 0xb76a49ac>

这按我想要的方式工作,打开了一个新窗口。

但我如何传递参数呢?就像,当终端启动时,我想让它说,运行ls。但这:

>>> subprocess.Popen(['gnome-terminal', 'ls'])
<subprocess.Popen object at 0xb76a706c>

这又有效,但 ls 命令不起作用:启动一个空白终端窗口。

所以我的问题是,如何使用指定的命令启动终端窗口,以便在窗口打开时运行该命令。

PS:我的目标 Linux。

Using Python , I would like to start a process in a new terminal window, because so as to show the user what is happening and since there are more than one processes involved.

I tried doing:

>>> import subprocess
>>> subprocess.Popen(['gnome-terminal'])
<subprocess.Popen object at 0xb76a49ac>

and this works as I want, a new window is opened.

But how do I pass arguments to this? Like, when the terminal starts, I want it to say, run ls. But this:

>>> subprocess.Popen(['gnome-terminal', 'ls'])
<subprocess.Popen object at 0xb76a706c>

This again works, but the ls command doesn't: a blank terminal window starts.

So my question is, how do I start the terminal window with a command specified, so that the command runs when the window opens.

PS: I am targetting only Linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽月 2024-10-17 16:10:48
$ gnome-terminal --help-all

 ...

  -e, --command                   Execute the argument to this option inside the terminal

 ...

如果您希望窗口保持打开状态,那么您需要运行一个 shell 或命令来使其保持打开状态。

$ gnome-terminal --help-all

 ...

  -e, --command                   Execute the argument to this option inside the terminal

 ...

If you want the window to stay open then you'll need to run a shell or command that keeps it open afterwards.

风渺 2024-10-17 16:10:48
In [5]: import subprocess

In [6]: import shlex

In [7]: subprocess.Popen(shlex.split('gnome-terminal -x bash -c "ls; read -n1"'))
Out[7]: <subprocess.Popen object at 0x9480a2c>
In [5]: import subprocess

In [6]: import shlex

In [7]: subprocess.Popen(shlex.split('gnome-terminal -x bash -c "ls; read -n1"'))
Out[7]: <subprocess.Popen object at 0x9480a2c>
意犹 2024-10-17 16:10:48

这是我用来从 WINE 中的 notepad++ 启动 gnome 终端的系统,

1:notepad++ 命令启动

#!/usr/bin/python
#this program takes three inputs:::
#$1 is the directory to change to (in case we have path sensitive programs)
#$2 is the linux program to run
#$3+ is the command line arguments to pass the program
#
#after changing directory, it launches a gnome terminal for the new spawned linux program
#so that your windows program does not eat all the stdin and stdout (grr notepad++)

import sys

import os
import subprocess as sp

dir = sys.argv[1]
dir = sp.Popen(['winepath','-u',dir], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1]

os.chdir(os.path.normpath(os.path.realpath(dir)))
print os.getcwd()

print "running '%s'"%sys.argv[2]
cmd=['gnome-terminal','-x','run_linux_program_sub']
for arg in sys.argv[2:]:
    cmd.append(os.path.normpath(os.path.realpath(sp.Popen(['winepath','-u',arg], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1])))
print cmd
p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE)

2: 运行子脚本,我用它在程序退出后运行 bash(在这种情况下通常是 python)

#!/bin/sh
#$1 is program to run, $2 is argument to pass
#afterwards, run bash giving me time to read terminal, or do other things
$1 "$2"
echo "-----------------------------------------------"
bash

this is the system that I use to launch a gnome-terminal from notepad++ in WINE,

1:notepad++ command to launch

#!/usr/bin/python
#this program takes three inputs:::
#$1 is the directory to change to (in case we have path sensitive programs)
#$2 is the linux program to run
#$3+ is the command line arguments to pass the program
#
#after changing directory, it launches a gnome terminal for the new spawned linux program
#so that your windows program does not eat all the stdin and stdout (grr notepad++)

import sys

import os
import subprocess as sp

dir = sys.argv[1]
dir = sp.Popen(['winepath','-u',dir], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1]

os.chdir(os.path.normpath(os.path.realpath(dir)))
print os.getcwd()

print "running '%s'"%sys.argv[2]
cmd=['gnome-terminal','-x','run_linux_program_sub']
for arg in sys.argv[2:]:
    cmd.append(os.path.normpath(os.path.realpath(sp.Popen(['winepath','-u',arg], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1])))
print cmd
p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE)

2: run sub script, which I use to run bash after my program quits (python in this case normally)

#!/bin/sh
#$1 is program to run, $2 is argument to pass
#afterwards, run bash giving me time to read terminal, or do other things
$1 "$2"
echo "-----------------------------------------------"
bash
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文