Prevent Terminal resize python curses

2024/7/5 11:59:01

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

`

Answer

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://en.xdnf.cn/q/119458.html

Related Q&A

SymPy Not Doesnt Return LaTeX

Helloo! So, Im using SymPy to make a calculation for me. The trouble is, its output should be a LaTeX expression and in make case it prints something like SymPy Calculation Output Is there any way to s…

Python Flask: How to include JavaScript file for each template per blueprint

I have read Loading external script with jinja2 template directive and Import javascript files with jinja from static folder but unfortunately no closer I have a Python Flask site which is based on htt…

Difference between multiple elements in list with same string . Python 2.7 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

EDX Course API: Getting EDX course list

I am making a project in python/flask. I want to get a list of all the courses of edx. But the API provides the list page by page. I cant figure out how to get the entire list. Any help is appreciated.…

How to extract particlar message from a vast displayed output using python regular expression?

Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number Also how to extract SUCCESS and FAILED message from the output (reference) For examp…

Enemy health bar aint draining pygame [duplicate]

This question already has answers here:How to put a health bar over the sprite in pygame(2 answers)Closed 3 years ago.Okay so I was trying to make a health bar for my enemy class and only a part of it …

Python - Create and instantiate class

I am building a class of playlists, which will hold many playlists of the same genre.class playlist(object):def __init__(self,name):self.name = nameI would like to instantiate them passing the user:def…

Missing samples of a dataframe in pandas

My df:In [163]: df.head() Out[163]: x-axis y-axis z-axis time 2017-07-27 06:23:08 -0.107666 -0.068848 0.963623 2017-07-27 06:23:08 -0.105225 -0.070068 0.963867 .....I set the index as dateti…

How to hide a button after clicked in Python

I was wondering how to hide my start button after being clicked so that If the user accidentally was clicker happy they wouldnt hit the button causing more bubbles to appear on screen. Below is a snipp…

Unable to click on QRadioButton after linking it with QtCore.QEventLoop()

Few days back i had situation where i had to check/uncheck QRadioButton in for loop. Here is the link Waiting in for loop until QRadioButton get checked everytime? After implementing QEventLoop on thi…