I am attempting to create an ASCII level editor in Python using curses
but I'm having issues. I get Traceback (most recent call last): File "lvleditor_curses.py", line 36, in <module> editor.newLine() File "lvleditor_curses.py", line 31, in newLine self.stdscr.addstr(self.level[0][0]) TypeError: expect bytes or str, got int
when using the following code.
import os, curses
class Editor:
def __init__(self):
self.stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
self.stdscr.keypad(True)
self.stdscr.addstr("test")
self.stdscr.refresh()
self.level = []
def newLine(self):
line = self.stdscr.getstr()
self.level += [list(line)]
self.stdscr.addstr(self.level[0][0])
self.stdscr.refresh()
editor = Editor()
editor.newLine()
Clearly, self.level[0][0]
is of the wrong type ( int
). Cast it to string with str(self.level[0][0])
.
I just had this issue now. The getstr
function returns a bytearray by default. Therefore you must cast the bytearray to a string. Add:
line = line.decode()
Under:
line = self.stdscr.getstr()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.