使用参数启动 gnome-terminal
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望窗口保持打开状态,那么您需要运行一个 shell 或命令来使其保持打开状态。
If you want the window to stay open then you'll need to run a shell or command that keeps it open afterwards.
这是我用来从 WINE 中的 notepad++ 启动 gnome 终端的系统,
1:notepad++ 命令启动
2: 运行子脚本,我用它在程序退出后运行 bash(在这种情况下通常是 python)
this is the system that I use to launch a gnome-terminal from notepad++ in WINE,
1:notepad++ command to launch
2: run sub script, which I use to run bash after my program quits (python in this case normally)