栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在新的控制台窗口中打开Python线程

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在新的控制台窗口中打开Python线程

重新检查您的问题,而不是使用控制台或终端窗口。您试图做的是创建一个GUI。有许多跨平台的工具包,包括Wx和Tkinter,它们的小部件可以完全满足您的需求。用于输出的文本框和用于读取键盘输入的输入小部件。另外,您可以将它们包装在带有标题,帮助,打开/保存/关闭等的漂亮框架中。

我同意@stark的GUI是这样的。

纯粹出于说明目的,这是一种不建议使用的非GUI方式,该方式显示了如何使用线程,子进程和命名管道(如IPC)来执行此操作。

有两个脚本:

entry.py
:接受用户的命令,对命令进行处理,然后将其传递给命令行中指定的命名管道:

#!/usr/bin/env pythonimport sysprint 'entry console'with open(sys.argv[1], 'w') as file:    for command in iter(lambda: raw_input('>>> '), ''):        print ''.join(reversed(command)) # do something with it        print >>file, command # pass the command to view window        file.flush()

view.py:启动入口控制台,在线程中打印常量更新,接受来自命名管道的输入,并将其传递给更新线程:

#!/usr/bin/env pythonimport osimport subprocessimport sysimport tempfilefrom Queue import Queue, Emptyfrom threading import Threaddef launch_entry_console(named_pipe):    if os.name == 'nt': # or use sys.platform for more specific names        console = ['cmd.exe', '/c'] # or something    else:        console = ['xterm', '-e'] # specify your favorite terminal # emulator here    cmd = ['python', 'entry.py', named_pipe]    return subprocess.Popen(console + cmd)def print_updates(queue):    value = queue.get() # wait until value is available    msg = ""    while True:        for c in "/-|": minwidth = len(msg) # make sure previous output is overwritten msg = "r%s %s" % (c, value) sys.stdout.write(msg.ljust(minwidth)) sys.stdout.flush() try:     value = queue.get(timeout=.1) # update value     print except Empty:     passprint 'view console'# launch updates threadq = Queue(maxsize=1) # use queue to communicate with the threadt = Thread(target=print_updates, args=(q,))t.daemon = True # die with the programt.start()# create named pipe to communicate with the entry consoledirname = tempfile.mkdtemp()named_pipe = os.path.join(dirname, 'named_pipe')os.mkfifo(named_pipe) #note: there should be an analog on Windowstry:    p = launch_entry_console(named_pipe)    # accept input from the entry console    with open(named_pipe) as file:        for line in iter(file.readline, ''): # pass it to 'print_updates' thread q.put(line.strip()) # block until the value is retrieved    p.wait()finally:    os.unlink(named_pipe)    os.rmdir(dirname)

要尝试,请运行:

$ python view.py


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号