为什么我的诅咒盒子不画?

2024-05-10 15:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在玩弄咒语,屏幕上连一个画框都画不出来。 我创建了一个有效的边框,但我想在边框中画一个框

这是我的密码

import curses 

screen = curses.initscr()

try:
    screen.border(0)
    box1 = curses.newwin(20, 20, 5, 5)
    box1.box()
    screen.getch()

finally:
    curses.endwin()

有什么建议吗?


Tags: importbox密码屏幕screencurses咒语边框
2条回答

建议的答案比必要的更复杂。如果您使用subwin,它将与原始窗口共享内存,并且将在无需额外工作的情况下重新绘制。

以下是修改后的原始程序(单行更改):

import curses

screen = curses.initscr()

try:
    screen.border(0)
    box1 = screen.subwin(20, 20, 5, 5)
    box1.box()
    screen.getch()

finally:
    curses.endwin()

来自curses docs

When you call a method to display or erase text, the effect doesn’t immediately show up on the display. ...

Accordingly, curses requires that you explicitly tell it to redraw windows, using the refresh() method of window objects. ...

您需要screen.refresh()box1.refresh()以正确的顺序。

工作实例

#!/usr/bin/env python

import curses 

screen = curses.initscr()

try:
    screen.border(0)

    box1 = curses.newwin(20, 20, 5, 5)
    box1.box()    

    screen.refresh()
    box1.refresh()

    screen.getch()

finally:
    curses.endwin()

或者

#!/usr/bin/env python

import curses 

screen = curses.initscr()

try:
    screen.border(0)
    screen.refresh()

    box1 = curses.newwin(20, 20, 5, 5)
    box1.box()    
    box1.refresh()

    screen.getch()

finally:
    curses.endwin()

您可以使用immedok(True)自动刷新窗口

#!/usr/bin/env python

import curses 

screen = curses.initscr()
screen.immedok(True)

try:
    screen.border(0)

    box1 = curses.newwin(20, 20, 5, 5)
    box1.immedok(True)

    box1.box()    
    box1.addstr("Hello World of Curses!")

    screen.getch()

finally:
    curses.endwin()

相关问题 更多 >