Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

linux - Prevent Terminal resize python curses

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

`

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...