【问题标题】:Python ncurses addstr expected bytes or str, got intPython ncurses addstr 预期字节或 str,得到 int
【发布时间】:2018-04-24 18:37:26
【问题描述】:

我正在尝试使用curses 在 Python 中创建一个 ASCII 级别的编辑器,但我遇到了问题。使用以下代码时,我得到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

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()

【问题讨论】:

  • 请以Minimal, Complete, and Verifiable example 的形式提供您的代码。如果您遇到错误,请将错误消息复制并粘贴到您的问题中。
  • 运行时self.level[0][0]的类型是什么? (type(self.level[0][0])
  • @TwistedSim 更新了我的帖子。
  • 请逐字复制并粘贴错误消息。

标签: python python-curses


【解决方案1】:

很明显,self.level[0][0] 的类型有误(int)。使用str(self.level[0][0]) 将其转换为字符串。

【讨论】:

  • 投射时会打印出 ASCII 值。
【解决方案2】:

我现在刚遇到这个问题。 getstr 函数默认返回一个字节数组。因此,您必须将字节数组转换为字符串。添加:

line = line.decode()

下:

line = self.stdscr.getstr()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 2019-03-18
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多