The program I'm trying to write prints 'Hello World' into the window.
When Hello World is clicked with the mouse it reads Hello World character by character
The cursor is then moved further down the screen and should display what it has read.
What is actually displayed is garbled text.
Hello world
^[[M %!^[[M#%!
It should be:
Hello world
Hello world
The code is shown below:
I'm only reading the string character by character because I could not find a way to read the whole string.
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()