Python Curses 处理窗口(终端)调整大小
这实际上是两个问题:
- 如何调整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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
终端调整大小事件将产生
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 withgetch
.我让我的 python 程序通过做几件事来重新调整终端的大小。
当我编写程序时,我可以看到将屏幕放入其自己的类中并定义所有这些函数的有用性,因此我所要做的就是调用
Screen.resize()
,这将需要照顾其余的。I got my python program to re-size the terminal by doing a couple of things.
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.我使用此处中的代码。
在我的curses脚本中,我不使用getch(),所以我无法对
KEY_RESIZE
做出反应。因此,脚本会对
SIGWINCH
作出反应,并在处理程序中重新初始化curses 库。当然,这意味着您必须重新绘制所有内容,但我找不到更好的解决方案。一些示例代码:
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:
当使用 curses.wrapper()< 时,这对我有用/a>:
This worked for me when using curses.wrapper():
这是不对的。这是一个
ncurses-only
扩展。该问题询问了curses
。要以符合标准的方式执行此操作,您需要自己捕获SIGWINCH
并安排屏幕重绘。It isn't right. It's an
ncurses-only
extension. The question asked aboutcurses
. To do this in a standards-conforming way you need to trapSIGWINCH
yourself and arrange for the screen to be redrawn.