我正在尝试编写的程序将“Hello World”打印到窗口中。
当用鼠标单击 Hello World 时,它会逐个字符地读取 Hello World
然后将光标向下移动到屏幕下方,并应显示它已读取的内容。
实际显示的是乱码。
Hello world
^[[M %!^[[M#%!
它应该是:
Hello world
Hello world
代码如下所示:
我只是逐个字符地读取字符串,因为我找不到读取整个字符串的方法。
import curses
# Initialize variables
q = -1
stdscr = curses.initscr()
curses.mousemask(1)
curses.start_color()
# color pair 1 = red text on white background
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
stdscr.bkgd(' ', curses.color_pair(1))
stdscr.addstr( "Hello world", curses.color_pair(1) )
# Move cursor furthur down screen and slightly to the right
stdscr.move(10,10)
while True:
event = stdscr.getch()
if event == ord('q'):
break # quit if q is pressed
if event == curses.KEY_MOUSE:
_, mx, my, _, _ = curses.getmouse()
y, x = stdscr.getyx() # put cursor position in x and y
# read the line character by character into char array called mystr
mystr = []
for i in range(140):
mystr.append(stdscr.inch(my, i)) # read char into mystr
stdscr.addstr(y, x, "".join(mystr)) # Try to display char array
# but it's garbled
stdscr.refresh()
curses.endwin()