Python: ncurses 不打印传递给函数的值
Python: ncurses not printing a value passed to function
我正在为应用程序编写 ncurses GUI。我在文件 admin.py
:
中有以下代码
#-*- coding: utf-8 -*-
import curses.textpad
from gui_helpers import *
import global_vars as g
def parse_command(win, command):
# print_str(win, 0, 0, command)
if command == "help":
print_str(win, 1, 1, "Available commands: add - adds a board, list - lists boards, mkdir - makes board directory")
def display_admin_screen(win, keypress):
rows = win.getmaxyx()[0]
cols = win.getmaxyx()[1]
print_str_center(win, 0, 1, "Admin interface", g.RED|g.BOLD)
print_str(win, 1, 0, "Command line")
textpad_win = win.subwin(1, cols - 1, 3, 1)
output_win = win.subwin(5, cols - 1, 5, 1)
output_win.refresh()
textpad_win.refresh()
win.refresh()
curses.curs_set(1)
textpad = curses.textpad.Textbox(textpad_win)
textpad.stripspaces = 0
textpad_win.move(0,0)
textpad.edit()
command = textpad.gather()
parse_command(output_win, command)
# print_str(output_win, 0, 0, command)
textpad_win.clear()
curses.curs_set(0)
代码本身(函数 display_admin_screen()
)在文件 gui.py
中循环运行,其中包含所有必要的库导入:
while True:
keypress = stdscr.getch()
display_title_bar(stdscr)
if g.STATE == 'motd':
display_motd(working_win, keypress)
elif g.STATE == 'boards':
boards_view.display_threads(working_win, g.SELECTED_BOARD, keypress)
elif g.STATE == 'admin':
admin.display_admin_screen(working_win, keypress)
working_win.refresh()
stdscr.refresh()
我遇到的问题是,当我在 admin.py
的文本框中键入 'help' 时,没有任何反应。它应该在 output_win
中显示帮助文本。我已检查该值是否已正确传递给函数 parse_command()
,并且它是一个正确的值。我认为这可能是在循环内创建 subwindow 的问题,所以我尝试在循环外的 gui.py
中创建它们并将它们传递给函数,但无济于事。如果我只是告诉 parse_command()
函数向输出 window 写入一些内容,那没问题。由于某种原因,if
块似乎有问题。
问题是您的程序使用
从标准屏幕读取
stdscr.getch()
对 stdscr
进行刷新,可以覆盖任何其他 windows(例如 output_win
)。要解决此问题,您应该使用 window 调用 .getch()
,您希望 is 已更新。
如果这样做很尴尬,您可以使用 .touchwin()
and wrefresh()
强制给定的 window 重新绘制。
我终于找到问题所在了。按下 Enter 后,命令中附加了一堆空格(不是换行符 \n
)。我所要做的就是改变:
command = textpad.gather()
至
command = textpad.gather().strip()
现在可以使用了。
我正在为应用程序编写 ncurses GUI。我在文件 admin.py
:
#-*- coding: utf-8 -*-
import curses.textpad
from gui_helpers import *
import global_vars as g
def parse_command(win, command):
# print_str(win, 0, 0, command)
if command == "help":
print_str(win, 1, 1, "Available commands: add - adds a board, list - lists boards, mkdir - makes board directory")
def display_admin_screen(win, keypress):
rows = win.getmaxyx()[0]
cols = win.getmaxyx()[1]
print_str_center(win, 0, 1, "Admin interface", g.RED|g.BOLD)
print_str(win, 1, 0, "Command line")
textpad_win = win.subwin(1, cols - 1, 3, 1)
output_win = win.subwin(5, cols - 1, 5, 1)
output_win.refresh()
textpad_win.refresh()
win.refresh()
curses.curs_set(1)
textpad = curses.textpad.Textbox(textpad_win)
textpad.stripspaces = 0
textpad_win.move(0,0)
textpad.edit()
command = textpad.gather()
parse_command(output_win, command)
# print_str(output_win, 0, 0, command)
textpad_win.clear()
curses.curs_set(0)
代码本身(函数 display_admin_screen()
)在文件 gui.py
中循环运行,其中包含所有必要的库导入:
while True:
keypress = stdscr.getch()
display_title_bar(stdscr)
if g.STATE == 'motd':
display_motd(working_win, keypress)
elif g.STATE == 'boards':
boards_view.display_threads(working_win, g.SELECTED_BOARD, keypress)
elif g.STATE == 'admin':
admin.display_admin_screen(working_win, keypress)
working_win.refresh()
stdscr.refresh()
我遇到的问题是,当我在 admin.py
的文本框中键入 'help' 时,没有任何反应。它应该在 output_win
中显示帮助文本。我已检查该值是否已正确传递给函数 parse_command()
,并且它是一个正确的值。我认为这可能是在循环内创建 subwindow 的问题,所以我尝试在循环外的 gui.py
中创建它们并将它们传递给函数,但无济于事。如果我只是告诉 parse_command()
函数向输出 window 写入一些内容,那没问题。由于某种原因,if
块似乎有问题。
问题是您的程序使用
从标准屏幕读取stdscr.getch()
对 stdscr
进行刷新,可以覆盖任何其他 windows(例如 output_win
)。要解决此问题,您应该使用 window 调用 .getch()
,您希望 is 已更新。
如果这样做很尴尬,您可以使用 .touchwin()
and wrefresh()
强制给定的 window 重新绘制。
我终于找到问题所在了。按下 Enter 后,命令中附加了一堆空格(不是换行符 \n
)。我所要做的就是改变:
command = textpad.gather()
至
command = textpad.gather().strip()
现在可以使用了。