简体   繁体   English

使用python打开终端

[英]Opening terminal using python

Currently I've been creating just a small program. 目前,我只是在创建一个小程序。 And one of the options is to start a counter. 一种选择是启动一个计数器。

Code for counter 柜台代码

from time import sleep
number = 0
while 1 == 1:
    sleep(1)
    print number
    number += 1

I want to be able to open this program in a new terminal window (Ubuntu) so I can keep the existing terminal open. 我希望能够在新的终端窗口(Ubuntu)中打开该程序,以便使现有终端保持打开状态。 Anyway to do this in python? 无论如何要在python中做到这一点?

Thanks 谢谢

Paul 保罗

EDIT I think I figured out one way to run it in the same terminal so it works! 编辑我想我想出了一种方法可以在同一终端上运行它,所以它可以工作!

os.system('python counter.py')

I simply used that, I thought if I were to do that before, when I used CTRL + C it would close down both programs but this seems to of worked. 我只是简单地使用了它,我以为如果我以前做过,当我使用CTRL + C时,它将关闭两个程序,但这似乎行得通。 Thanks for the answers! 感谢您的回答!

You could create a wrapper that launches your terminal and tells it to run your script. 您可以创建一个包装程序,以启动您的终端并告诉其运行脚本。 If you know which terminal you're using, this isn't too hard. 如果您知道使用的是哪个终端,这并不太难。 For example, if you use multi-gnome-terminal : 例如,如果您使用multi-gnome-terminal

#!/bin/sh
multi-gnome-terminal --use-factory --command /usr/bin/python /path/to/my/script.py

Now, every time you run that wrapper (with sh ./wrapper , or /usr/local/bin/wrapper if you install -m755 it, or by double-clicking it, or whatever), it will open a new window (launching a new terminal process only if necessary) and run your script there. 现在,每次运行该包装程序时(如果install -m755则使用sh ./wrapper/usr/local/bin/wrapper ,或双击它或其他方法),它将打开一个新窗口(启动)一个新的终端进程(仅在必要时进行))并在那里运行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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