所以我想出了这个,但作为免责声明,我不知道这是否是线程安全的(但到目前为止没有问题)。
可以使用 python 库io捕获 print 的输出,更具体地说StringIO
是从该库中捕获。
注意这是针对 Python3
本质上,解决方案是设置sys.stdout
一个实例io.StringIO
并从中读取。
external_output = None
stdout_buff = io.StringIO()
sys.stdout = stdout_buff
stream_pos = 0 # lst read position of the stdout stream.
while True: #input loop
...
if stdout_buff.tell() > stream_pos:
stdout_buff.seek(stream_pos)
external_output = stdout_buff.read()
stream_pos = stdout_buff.tell()
...
下面我包含了一个我正在使用的菜单系统的简短示例,以防遇到此问题的任何人都不清楚以上内容,希望这可以解决问题。
干杯!
未修改版本
所以菜单的显示和事件循环过去看起来很像这样:(请注意,这是一个简化的版本,因此与显示菜单和显示用户键入的内容有很大关系)。这个基本示例显示了一个菜单,并允许用户退出程序,在他们的选择中输入数字,或者输入他们的选择,然后打印出来。
import sys
import curses
def menu(stdscr):
# initial startup settings
curses.start_color()
curses.use_default_colors()
stdscr.timeout(1000) #timeout the input loop every 1000 milliseconds
user_selection = ''
# other unrelated initial variables
while True: #display loop
stdscr.clear()
# the following is actually in a function to handle automatically
# taking care of fitting output to the screen and keeping
# track of line numbers, etc. but for demonstration purposes
# I'm using the this
start_y = 0
stdscr.addstr(start_y, 0, 'Menu Options:')
stdscr.addstr(start_y+1, 0, '1) option 1')
stdscr.addstr(start_y+2, 0, '1) option 2')
stdscr.addstr(start_y+3, 0, '1) option 3')
stdscr.addstr(start_y+4, 0, '1) option 4')
while True: #input loop
c = stdscr.getkey()
if c == 'KEY_RESIZE':
handle_window_resize() # handle changing stored widths and height of window
break #break to redraw screen
elif c.isdigit():
# if user typed a digit, add that to the selection string
# users may only select digits as their options
user_selection += c
elif c == '\n':
# user hit enter to submit their selection
if len(user_selection) > 0:
return user_selection
elif c == 'q':
sys.exit()
result = curses.wrapper(menu)
print(result)
在此示例中,仍然会出现问题,即与该线程同时运行的线程的任何输出都将打印在stdscr
程序当前等待用户输入的光标处。
修改版
import sys
import curses
from io import StringIO
def menu(stdscr):
# initial startup settings
curses.start_color()
curses.use_default_colors()
stdscr.timeout(1000) #timeout the input loop every 1000 milliseconds
user_selection = ''
# other unrelated initial variables
# output handling variables
external_output = None # latest output from stdout
external_nlines = 2 # number of lines at top to leave for external output
stdout_buff = StringIO()
sys.stdout = stdout_buff
stream_pos = 0 # lst read position of the stdout stream.
while True: #display loop
stdscr.clear()
# the following is actually in a function to handle automatically
# taking care of fitting output to the screen and keeping
# track of line numbers, etc. but for demonstration purposes
# I'm using the this
if external_output is not None:
stdscr.addstr(0, 0, "stdout: " + external_output)
start_y = external_nlines
stdscr.addstr(start_y, 0, 'Menu Options:')
stdscr.addstr(start_y+1, 0, '1) option 1')
stdscr.addstr(start_y+2, 0, '1) option 2')
stdscr.addstr(start_y+3, 0, '1) option 3')
stdscr.addstr(start_y+4, 0, '1) option 4')
while True: #input loop
try:
c = stdscr.getkey()
except:
c = -1 # 1000ms timeout or error
if c == -1:
if stdout_buff.tell() > stream_pos:
# current stdout_buff pos is greater than last read
# stream position, so there is unread output
stdout_buff.seek(stream_pos)
external_output = stdout_buff.read().strip() #strip whitespace
stream_pos = stdout_buff.tell() #set stream_pos to end of stdout_buff
break #redraw screen with new output
elif c == 'KEY_RESIZE':
handle_window_resize() # handle changing stored widths and height of window
break #break to redraw screen
elif c.isdigit():
# if user typed a digit, add that to the selection string
# users may only select digits as their options
user_selection += c
elif c == '\n':
# user hit enter to submit their selection
if len(user_selection) > 0:
sys.stdout = sys.__stdout__ # reset stdout to normal
return user_selection
elif c == 'q':
sys.stdout = sys.__stdout__ # reset stdout to normal
sys.exit()
result = curses.wrapper(menu)
print(result)