python多控制台窗口_python – 如何从单个脚本中打开两个控制台

如果你不想像

reconsider your problem and use a GUI这样的

reconsider your problem and use a GUI那么你可以使用子进程模块同时启动两个新的控制台并在打开的窗口中显示两个给定的字符串:

#!/usr/bin/env python3

import sys

import time

from subprocess import Popen, PIPE, CREATE_NEW_CONSOLE

messages = 'This is Console1', 'This is Console2'

# open new consoles

processes = [Popen([sys.executable, "-c", """import sys

for line in sys.stdin: # poor man's `cat`

sys.stdout.write(line)

sys.stdout.flush()

"""],

stdin=PIPE, bufsize=1, universal_newlines=True,

# assume the parent script is started from a console itself e.g.,

# this code is _not_ run as a *.pyw file

creationflags=CREATE_NEW_CONSOLE)

for _ in range(len(messages))]

# display messages

for proc, msg in zip(processes, messages):

proc.stdin.write(msg + "\n")

proc.stdin.flush()

time.sleep(10) # keep the windows open for a while

# close windows

for proc in processes:

proc.communicate("bye\n")

这是一个不依赖于CREATE_NEW_CONSOLE的简化版本:

#!/usr/bin/env python

"""Show messages in two new console windows simultaneously."""

import sys

import platform

from subprocess import Popen

messages = 'This is Console1', 'This is Console2'

# define a command that starts new terminal

if platform.system() == "Windows":

new_window_command = "cmd.exe /c start".split()

else: #XXX this can be made more portable

new_window_command = "x-terminal-emulator -e".split()

# open new consoles, display messages

echo = [sys.executable, "-c",

"import sys; print(sys.argv[1]); input('Press Enter..')"]

processes = [Popen(new_window_command + echo + [msg]) for msg in messages]

# wait for the windows to be closed

for proc in processes:

proc.wait()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值