Using the curses.panel module, a dialog box is curses.panel . The subwin this panel has an area where the text message from the external variable is displayed. If the text message does not contain new lines and exceeds the size of the text area, then everything goes without problems (as the text is added via addnstr , which limits the length of the entire text, in accordance with the size of the panel window). But if the variable with the message contains new lines (' \n ') - then in the case when the message does not fit the dimensions of the text area - an error occurs.

Here is the function itself:

 def Panel(screen, h, w, y, x, text): new_window = curses.newwin(h, w, y, x) new_window.erase() new_window.box() sub_window = new_window.subwin(h - 2, w - 2 , y + 1 , x + 1) sub_window.addnstr(0, 0, text, ((h - 2) * (w - 2) - 1)) panel = curses.panel.new_panel(new_window) curses.panel.update_panels() screen.refresh() screen.getch() 

The variables h and w passed to the function (height and width of the window) are dynamically set. That is, an error almost always occurs if there is a new line and a small terminal window.

What is the easiest way to solve this problem?

I understand that you can write a separate function replacing ' \n ' with spaces, depending on the width of the window, but can there be any regular solution or something simpler and more elegant?

    1 answer 1

    It turned out that using the insstr method instead of addnstr solves a problem inserted in the insstr string, no matter how insstr it is - does not cause an exception:

     def Panel(screen, h, w, y, x, text): new_window = curses.newwin(h, w, y, x) new_window.erase() new_window.box() sub_window = new_window.subwin(h - 2, w - 2 , y + 1 , x + 1) sub_window.insstr(0, 0, text) panel = curses.panel.new_panel(new_window) curses.panel.update_panels() screen.refresh() screen.getch()