문제 설명
Python 저주 update_panels() 및 Panel.move() 문제 (Python Curses update_panels() and Panel.move() issues)
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
무엇이 잘못된 것인지 알아내느라 고생했지만 아무데도 가고 있지 않으며 어떤 지침이라도 매우 감사할 것입니다.
표준 Windows 명령 프롬프트( x64 Windows 10 Pro(v10.0.18362) 시스템의 v10.0.18362.449)
참조 솔루션
방법 1:
ncurses' move_panel
function returns the result from mvwin
, which is documented as follows:
Calling
mvwin
moves the window so that the upper left‑hand corner is at position (x, y). If the move would cause the window to be off the screen, it is an error and the window is not moved. Moving subwindows is allowed, but should be avoided.
In python, you'd have to use a try/except block to avoid simply exiting when there is an error‑return from ncurses.
Using "windows‑curses", the underlying library is probably PDCurses (which has no documentation aside from comments in the source‑code). However, its panel‑support ultimately came from the same (Warren Tucker) implementation. You can see the error‑returns in the source‑code.
For what it's worth, the original panel library (author unknown) that one might have encountered in a vendor Unix machine (such as Solaris) did the same thing (see source on Illumos‑gate), but the documentation provided could not have been useful when Tucker developed a clone (see "documentation").
(by abmx、Thomas Dickey)