我正在嘗試製作一個程序,該程序將同時啓動視圖窗口(控制檯)和命令行。在視圖窗口中,它會顯示常量更新,而命令行窗口將使用raw_input()
接受影響視圖窗口的命令。我正在考慮爲此使用線程,但我不知道如何在新的控制檯窗口中啓動線程。我會怎麼做?在新的控制檯窗口中打開Python線程
回答
而不是使用控制檯或終端窗口,重新檢查您的問題。你要做的是創建一個GUI。有許多跨平臺的工具包,包括Wx和Tkinter,它們都有部件可以完成你想要的功能。用於輸出的文本框和用於閱讀鍵盤輸入的輸入小部件。另外,你可以用標題,幫助,打開/保存/關閉等方式將它們包裝在一個很好的框架中。
我同意@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
我想我已經理解了你的代碼的要點。所以可以肯定的是,當你啓動子進程'p = launch_entry_console(named_pipe)',然後開始用'open(named_pipe)as file'讀取文件時,這個讀取是否完成,因爲'p'在後臺運行?如在,我已經可以使用這種方案,如果我想用一個打開的管道讀取某個文本「停止」時終止「p」。 – 2016-04-15 10:32:01
是的,當您從命名管道讀取數據時,「p」進程正在運行。 – jfs 2016-04-15 10:39:03
在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
- 1. 如何打開Visual C++控制檯應用程序的新控制檯窗口?
- 2. 在單獨的控制檯窗口中打開應用程序
- 3. 如何打開程序中的空控制檯窗口(Windows)
- 4. 控制檯在新窗口中
- 5. 在python中打開新窗口(如vim)
- 6. Sat4j遠程控制窗口未打開
- 7. 打開控制檯時絕對位置div不在窗口中
- 8. ASP.NET控件在同一窗口中打開新的窗口intead
- 9. Eclipse在單獨的窗口中打開控制檯應用程序
- 10. 在新窗口中打開SAS程序
- 11. 保持控制檯窗口打開在Visual Studio(C)
- 12. 如何在執行後保持控制檯窗口打開?
- 13. 強制頁面在新窗口中打開selenium-web-driver python
- 14. 如何讓我的控制檯窗口打開? VB
- 15. 無法打開MySQL的控制檯窗口10
- 16. 在當前控制檯窗口中創建新進程
- 17. 用C#中的每個新線程打開一個新控制檯?
- 18. 在沒有可見控制檯窗口的窗口中創建後臺進程
- 19. 打開一個線程內的窗口
- 20. 打開新窗口
- 21. 打開新窗口
- 22. 打開新窗口
- 23. 使用java更新控制檯窗口
- 24. 更新和多個控制檯窗口
- 25. 控制檯菜單更新OpenGL窗口
- 26. 有沒有辦法設置窗口讓控制檯窗口打開?
- 27. C++關閉文本文件並打開新的控制檯窗口
- 28. 使用清單打開新控制檯窗口提升UAC的權限
- 29. 如何從Powershell打開Powershell控制檯窗口
- 30. 保持控制檯窗口打開使用C#
不知道,如果你能在所有的,但也有平臺之間的巨大差異。最重要的是,Windows控制檯與UNIX終端不同。你在哪個平臺上? – 2012-07-29 20:29:30
我想如何在Windows和UNIX/Linux/Mac上做到這一點,並使用sys.platform進行移植。 – elijaheac 2012-07-29 20:38:06
寫入更新的程序來自哪裏?你能控制它嗎? – jfs 2012-07-29 20:54:43