我正在尝试使用python随附的curses模块编写一个简单的CLI python文本编辑器。我已经在Windows 10机器上正确设置了所有内容,并且可以顺利完成所有教程。
我按顺序绘制屏幕,屏幕和屏幕上边框小于屏幕的窗口。使用从屏幕外部开始的两行,两列的边界测量值创建一个窗口。然后定义一个文本区域并创建一个子窗口。
如果我排除子窗口,则会绘制一个边框并将光标放置在边框内。但是,当我尝试通过收集文本来保存文本时,文本会包含边框。因此,这就是创建子窗口的原因。如果他的框大于子窗口,为什么边框会消失。
代码如下:
import curses
import traceback
from curses.textpad import Textbox, rectangle
from pathlib import Path
def draw_menu():
pass
def edit_mode(window):
textbox = Textbox(window)
textbox.edit()
return textbox.gather()
def main(screen):
"""
Get the screen size and set a border around the text area.
Reset the cursor to the top of the text area.
"""
# get the current screen size
y_max, x_max = screen.getmaxyx()
ymin_border, xmin_border, ymax_border, xmax_border = \
2, 2, y_max-2, x_max-2
ymin_textpad, xmin_textpad, ymax_textpad, xmax_textpad = \
ymin_border+1, xmin_border+1, ymax_border-1, xmax_border-1
# draw the windows and box around it. Place cursor at beginning of
# the edit area
window = curses.newwin(ymax_border, xmax_border, ymin_border, xmin_border)
window.box()
# if this line is excluded I see the box but then the box is saved input
text_box = screen.subwin(ymax_textpad, xmax_textpad, ymin_textpad,
xmin_textpad)
window.addstr(ymin_textpad, xmin_textpad, "")
screen.refresh()
text = edit_mode(text_box) # right now drop user into edit mode
return text
if __name__ == "__main__":
buffer = ""
try:
screen = curses.initscr()
curses.noecho()
buffer = main(screen)
curses.echo()
curses.endwin()
print(f"Saved Buffer to Screen\n{buffer}")
except:
curses.echo()
curses.endwin()
traceback.print_exc()