Prevent Terminal resize python curses

旧街凉风 提交于 2019-12-26 15:26:05

问题


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()

`


回答1:


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:

  • C ncurses prevent resize, which gives the same caveat
  • How to prevent screen from resizing my Terminal in Mac OS X?, noting one special case where resizing can be disabled
  • How to prevent Terminal from resizing when font size is changed, which disagrees with that, and mentions another special case.

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 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.

Regarding the test program added early June 2:

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).



来源:https://stackoverflow.com/questions/30571686/prevent-terminal-resize-python-curses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!