【问题标题】:Python Curses update_panels() and Panel.move() issuesPython 诅咒 update_panels() 和 Panel.move() 问题
【发布时间】:2020-02-15 02:46:01
【问题描述】:

我正在尝试学习如何使用 Python Curses(并希望构建一个简单的游戏),但不知何故我无法让 curses 面板工作。

具体来说,当我尝试 update_panels() 时,我的脚本(如下)会立即直接退出并返回提示符。

import curses, curses.panel, time as t

def main(stdscr):
    # Initialize some colors and styles
    curses.start_color()
    curses.init_pair( 1, curses.COLOR_RED, curses.COLOR_BLACK )
    pl_style = curses.color_pair( 1 ) + curses.A_BOLD + curses.A_REVERSE

    # Create a Background window and panel
    bg_W = curses.newwin( 10, 100, 5, 5 )
    bg_W.box(); 
    bg_W.addstr( 1, 2, "BG Window" )
    bg_P = curses.panel.new_panel( bg_W )
    bg_W.refresh(); t.sleep( 2 )

    # Create a Player window and panel
    pl_W = curses.newwin( 1, 1, 10, 20 )
    pl_W.insch( '+', pl_style )
    pl_P = curses.panel.new_panel( pl_W )
    pl_W.refresh(); t.sleep( 2 )

    # Update panels
    curses.panel.update_panels()
    curses.doupdate()

    # # Move the Player panel and update panels
    # pl_P.move( 10, 30 )
    # curses.panel.update_panels()
    # curses.doupdate()
    # t.sleep( 2 )

    while True: 
        if pl_W.getch() == 27: break

if __name__ == "__main__": 
    curses.wrapper( main )

或者,当我尝试仅移动面板时,我的脚本(如下)返回错误。

import curses, curses.panel, time as t

def main(stdscr):
    # Initialize some colors and styles
    curses.start_color()
    curses.init_pair( 1, curses.COLOR_RED, curses.COLOR_BLACK )
    pl_style = curses.color_pair( 1 ) + curses.A_BOLD + curses.A_REVERSE

    # Create a Background window and panel
    bg_W = curses.newwin( 10, 100, 5, 5 )
    bg_W.box(); 
    bg_W.addstr( 1, 2, "BG Window" )
    bg_P = curses.panel.new_panel( bg_W )
    bg_W.refresh(); t.sleep( 2 )

    # Create a Player window and panel
    pl_W = curses.newwin( 1, 1, 10, 20 )
    pl_W.insch( '+', pl_style )
    pl_P = curses.panel.new_panel( pl_W )
    pl_W.refresh(); t.sleep( 2 )

    # # Update panels
    # curses.panel.update_panels()
    # curses.doupdate()

    # Move the Player panel and update panels
    pl_P.move( 10, 30 )
    curses.panel.update_panels()
    curses.doupdate()
    t.sleep( 2 )

    while True: 
        if pl_W.getch() == 27: break

if __name__ == "__main__": 
    curses.wrapper( main )

我得到的错误如下:

Traceback (most recent call last):
  File "panl2.py", line 36, in <module>
    curses.wrapper( main )
  File "C:\Program Files\Python38\lib\curses\__init__.py", line 105, in wrapper
    return func(stdscr, *args, **kwds)
  File "panl2.py", line 27, in main
    pl_P.move( 10, 30 )
_curses_panel.error: move_panel() returned ERR

我花了很长时间试图找出问题所在,但绝对无处可去,非常感谢任何指导。

我在 x64 Windows 10 Pro (v10.0.18362) 机器上的标准 Windows 命令提示符 (v10.0.18362.449) 中使用 windows-curses (v2.1.0) 和 Python 3.8 (v3.8.1:1b293b6) .

【问题讨论】:

    标签: python panel ncurses curses


    【解决方案1】:

    ncurses 的move_panel 函数返回来自mvwin 的结果,记录如下:

    调用 mvwin 移动窗口,使左上角位于 位置 (x, y)。如果移动会导致窗口关闭 屏幕,这是一个错误,窗口没有移动。移动子窗口 是允许的,但应该避免。

    在 python 中,您必须使用 try/except 块来避免在 ncurses 返回错误时简单地退出。

    使用“windows-curses”,底层库可能是 PDCurses(除了源代码中的 cmets 之外没有其他文档)。然而,它的面板支持最终来自相同的 (Warren Tucker) 实现。您可以在source-code 中看到错误返回。

    对于它的价值,人们可能在供应商 Unix 机器(例如 Solaris)中遇到的原始面板库(作者未知)做了同样的事情(参见 Illumos-gate 上的 source),但提供的文档Tucker 开发克隆时可能没有用处(请参阅"documentation")。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 2014-04-23
      • 2013-11-22
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2022-08-14
      相关资源
      最近更新 更多