2012-07-29 69 views
7

我正在尝试制作一个程序,该程序将同时启动视图窗口(控制台)和命令行。在视图窗口中,它会显示常量更新,而命令行窗口将使用raw_input()接受影响视图窗口的命令。我正在考虑为此使用线程,但我不知道如何在新的控制台窗口中启动线程。我会怎么做?在新的控制台窗口中打开Python线程

+3

不知道,如果你能在所有的,但也有平台之间的巨大差异。最重要的是,Windows控制台与UNIX终端不同。你在哪个平台上? – 2012-07-29 20:29:30

+0

我想如何在Windows和UNIX/Linux/Mac上做到这一点,并使用sys.platform进行移植。 – elijaheac 2012-07-29 20:38:06

+0

写入更新的程序来自哪里?你能控制它吗? – jfs 2012-07-29 20:54:43

回答

4

而不是使用控制台或终端窗口,重新检查您的问题。你要做的是创建一个GUI。有许多跨平台的工具包,包括Wx和Tkinter,它们都有部件可以完成你想要的功能。用于输出的文本框和用于阅读键盘输入的输入小部件。另外,你可以用标题,帮助,打开/保存/关闭等方式将它们包装在一个很好的框架中。

9

我同意@stark是一种GUI。

纯粹为了说明,这里是一个不建议使用非GUI的方式,显示如何使用线程,子进程和命名管道作为IPC。

两个脚本:

  • entry.py:从用户接受命令,做一些事情的命令,它通过在命令行给出的命名管道:

    #!/usr/bin/env python 
    import sys 
    
    print '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 python 
    import os 
    import subprocess 
    import sys 
    import tempfile 
    from Queue import Queue, Empty 
    from threading import Thread 
    
    def 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: 
           pass 
    
    print 'view console' 
    # launch updates thread 
    q = Queue(maxsize=1) # use queue to communicate with the thread 
    t = Thread(target=print_updates, args=(q,)) 
    t.daemon = True # die with the program 
    t.start() 
    
    # create named pipe to communicate with the entry console 
    dirname = tempfile.mkdtemp() 
    named_pipe = os.path.join(dirname, 'named_pipe') 
    os.mkfifo(named_pipe) #note: there should be an analog on Windows 
    try: 
        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 
+0

我想我已经理解了你的代码的要点。所以可以肯定的是,当你启动子进程'p = launch_entry_console(named_pipe)',然后开始用'open(named_pipe)as file'读取文件时,这个读取是否完成,因为'p'在后台运行?如在,我已经可以使用这种方案,如果我想用一个打开的管道读取某个文本“停止”时终止“p”。 – 2016-04-15 10:32:01

+0

是的,当您从命名管道读取数据时,“p”进程正在运行。 – jfs 2016-04-15 10:39:03

+0

在Windows中,您可以使用'console = [“cmd.exe”,“/ c”,“start”]'。 'start'使Windows打开一个新窗口。另外,我更喜欢引用COMSPEC环境变量并使用大写字母,如'console = [os.environ.get(“COMSPEC”,“CMD.EXE”),“/ C”,“START”]',但这只是我的风格。当然,我同意GUI比所有这些都更好。 – wecsam 2017-07-18 12:10:30

相关问题