防止终端调整 python 诅咒 [英] Prevent Terminal resize python curses

查看:88
本文介绍了防止终端调整 python 诅咒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写关于 python curses 的程序,我想知道是否有一种方法可以阻止终端调整大小以防止 Curses 在 Linux 和 Windows 上崩溃.这就是发生的事情.. 我可以防止这种情况吗?在 Windows 下,这不会发生,因为窗口大小调整不会影响提示比例...http://alessio.ragno.info/Before%20Resize.pnghttp://alessio.ragno.info/After%20Resize.png

I'm writing a program on python curses and I was wondering if there is a way to block terminal resizing in order to prevent curses' crashing both on Linux and Windows. This is what happens.. Can I prevent this? Under Windows this does not happen cause window resizing doesn't influence prompt proportions... http://alessio.ragno.info/Before%20Resize.png http://alessio.ragno.info/After%20Resize.png

`

import curses
screenTest = curses.initscr()
boxTest = curses.newwin(24,80,0,0)
boxTest.box()
curses.noecho()
curses.cbreak()
curses.start_color()
screenTest.keypad(1)
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
curses.init_pair(2,curses.COLOR_YELLOW, curses.A_NORMAL)
boxTest.border(0)
boxTest.addstr(1,1,78*"_")
boxTest.addstr(10,10,"Press ESC to quit...")
boxTest.refresh()
x = screenTest.getch()
while x != 27:
    boxTest.addstr(1,1,78*"_")
    boxTest.addstr(10,10,"Press ESC to quit...")
    boxTest.refresh()
    x = screenTest.getch()

curses.endwin()
exit()

`

推荐答案

终端会调整大小,因为用户会这样做.没有通用的方法可以防止这种情况:抑制窗口装饰以消除调整大小的手柄是不便携的部分解决方案.您可能偶尔会发现有关此问题不同方面的评论,但没有全面的解决方案.这里有几个链接:

Terminals are going to resize because users do that. There is no general way to prevent that: suppressing window decorations to eliminate the resize grips is a partial solution which is not portable. You may find occasional comments about different aspects of this, but no comprehensive solutions. Here are a few links:

在curses/ncurses(而不是PDCurses)中,库会注意环境变量LINESCOLUMNS,除非使用use_env 函数.ncurses 在初始化期间为 SIGWINCH 添加了一个信号处理程序,用于检测(并响应)窗口大小调整(请参阅 resizeterm,例如).如果设置了这些环境变量,ncurses 将不会响应.然而(见下文)改变这种特殊情况不会让 Python 停止崩溃,因为 Python 有更多的问题.

In curses/ncurses (not in PDCurses), the library pays attention to the environment variables LINES and COLUMNS unless disabled with the use_env function. ncurses adds a signal handler for SIGWINCH during initialization which it uses to detect (and respond to) window-resizing (see resizeterm, for example). If these environment variables set, ncurses will not respond. However (see below) changing this special case will not make Python stop crashing, because Python has more problems than that.

ncurses 已经处理了近 20 年的窗口大小调整;有应用程序无法使用此功能.没有具体的测试程序来讨论,就无法确定程序崩溃的原因.

ncurses has handled resizing of windows for almost 20 years; there are applications which fail to work with this. Without some specific test program to discuss, there is no way to determine the cause of a program's crashing.

关于 6 月初添加的测试程序:

Regarding the test program added early June 2:

使用 valgrind 运行您的测试程序,我看到许多错误几乎都在 Python 本身中(Debian 6 和 8 上的 2.6.6 和 2.7.9),还有一小部分Python 正在释放 ncurses 拥有的内存.在每种情况下,Python 都在释放已经释放的内存.即使不调整大小,它也会这样做(尽管一些不正确的释放与调整大小有关).我运行了几次,至少有一次 none 检测到的错误都在 ncurses 中.根据 valgrind 的说法,Python 中有几十个点会产生这些错误,其中一些占它检测到的 400 多次发生的 2/3.所以这个问题是错误的.(我可以建议对测试程序本身进行一些改进,但似乎真正的问题是 Python).

Running your test-program with valgrind, I see a number of errors which are almost all in Python itself (2.6.6 and 2.7.9 on Debian 6 and 8), and a small fraction where Python is freeing memory owned by ncurses. In each case, Python is freeing memory which was already freed. It does this even without resizing (though some of the incorrect frees are related to resizing). I ran this several times, and at least once none of the errors detected were in ncurses. According to valgrind, there are a few dozen points in Python which produce these errors, a few of those account for 2/3 of the more than 400 occurrences it detected. So the question is misphrased. (I could suggest some improvements to the test program itself, but it seems the real problem is Python).

这篇关于防止终端调整 python 诅咒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆