Python Curses 处理窗口(终端)调整大小

发布于 2024-10-19 20:03:37 字数 258 浏览 2 评论 0原文

这实际上是两个问题:

  • 如何调整curses窗口的大小,以及
  • 如何处理curses中的终端大小调整?

是否可以知道窗口大小何时改变?

我真的找不到任何好的文档,甚至没有在 http://docs.python.org 上找到/library/curses.html

This is two questions really:

  • how do I resize a curses window, and
  • how do I deal with a terminal resize in curses?

Is it possible to know when a window has changed size?

I really can't find any good doc, not even covered on http://docs.python.org/library/curses.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦亦如幻 2024-10-26 20:03:37

终端调整大小事件将产生 curses.KEY_RESIZE 键代码。因此,您可以将终端大小调整作为curses 程序中标准主循环的一部分来处理,等待使用getch 输入。

Terminal resize event will result in the curses.KEY_RESIZE key code. Therefore you can handle terminal resize as part of a standard main loop in a curses program, waiting for input with getch.

情痴 2024-10-26 20:03:37

我让我的 python 程序通过做几件事来重新调整终端的大小。

# Initialize the screen
import curses

screen = curses.initscr()

# Check if screen was re-sized (True or False)
resize = curses.is_term_resized(y, x)

# Action in loop if resize is True:
if resize is True:
    y, x = screen.getmaxyx()
    screen.clear()
    curses.resizeterm(y, x)
    screen.refresh()

当我编写程序时,我可以看到将屏幕放入其自己的类中并定义所有这些函数的有用性,因此我所要做的就是调用 Screen.resize() ,这将需要照顾其余的。

I got my python program to re-size the terminal by doing a couple of things.

# Initialize the screen
import curses

screen = curses.initscr()

# Check if screen was re-sized (True or False)
resize = curses.is_term_resized(y, x)

# Action in loop if resize is True:
if resize is True:
    y, x = screen.getmaxyx()
    screen.clear()
    curses.resizeterm(y, x)
    screen.refresh()

As I'm writing my program I can see the usefulness of putting my screen into it's own class with all of these functions defined so all I have to do is call Screen.resize() and it would take care of the rest.

鲜肉鲜肉永远不皱 2024-10-26 20:03:37

我使用此处中的代码。

在我的curses脚本中,我不使用getch(),所以我无法对KEY_RESIZE做出反应。

因此,脚本会对 SIGWINCH 作出反应,并在处理程序中重新初始化curses 库。当然,这意味着您必须重新绘制所有内容,但我找不到更好的解决方案。

一些示例代码:

from curses import initscr, endwin
from signal import signal, SIGWINCH
from time import sleep

stdscr = initscr()

def redraw_stdscreen():
    rows, cols = stdscr.getmaxyx()
    stdscr.clear()
    stdscr.border()
    stdscr.hline(2, 1, '_', cols-2)
    stdscr.refresh()

def resize_handler(signum, frame):
    endwin()  # This could lead to crashes according to below comment
    stdscr.refresh()
    redraw_stdscreen()

signal(SIGWINCH, resize_handler)

initscr()

try:
    redraw_stdscreen()

    while 1:
        # print stuff with curses
        sleep(1)
except (KeyboardInterrupt, SystemExit):
    pass
except Exception as e:
    pass

endwin()

I use the code from here.

In my curses-script I don't use getch(), so I can't react to KEY_RESIZE.

Therefore the script reacts to SIGWINCH and within the handler re-inits the curses library. That means of course, you'll have to redraw everything, but I could not find a better solution.

Some example code:

from curses import initscr, endwin
from signal import signal, SIGWINCH
from time import sleep

stdscr = initscr()

def redraw_stdscreen():
    rows, cols = stdscr.getmaxyx()
    stdscr.clear()
    stdscr.border()
    stdscr.hline(2, 1, '_', cols-2)
    stdscr.refresh()

def resize_handler(signum, frame):
    endwin()  # This could lead to crashes according to below comment
    stdscr.refresh()
    redraw_stdscreen()

signal(SIGWINCH, resize_handler)

initscr()

try:
    redraw_stdscreen()

    while 1:
        # print stuff with curses
        sleep(1)
except (KeyboardInterrupt, SystemExit):
    pass
except Exception as e:
    pass

endwin()
流年里的时光 2024-10-26 20:03:37

当使用 curses.wrapper()< 时,这对我有用/a>:

if stdscr.getch() == curses.KEY_RESIZE:
    curses.resizeterm(*stdscr.getmaxyx())
    stdscr.clear()
    stdscr.refresh()

This worked for me when using curses.wrapper():

if stdscr.getch() == curses.KEY_RESIZE:
    curses.resizeterm(*stdscr.getmaxyx())
    stdscr.clear()
    stdscr.refresh()
紫罗兰の梦幻 2024-10-26 20:03:37

这是不对的。这是一个 ncurses-only 扩展。该问题询问了curses。要以符合标准的方式执行此操作,您需要自己捕获 SIGWINCH 并安排屏幕重绘。

It isn't right. It's an ncurses-only extension. The question asked about curses. To do this in a standards-conforming way you need to trap SIGWINCH yourself and arrange for the screen to be redrawn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文