0

I have been working on a terminal GUI using python and Curses for the past few days and feel I am not making any progress. Right now what I need to do is create two panels that both take up half the screen minus 1 - 2 characters as a margin. I want these windows to be able to handle resizing so if the user decides to make their screen larger or smaller my panels resize to adjust to the user's needs.

+---------------------------+ +---------------------------+
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
|                           | |                           |
+---------------------------+ +---------------------------+

At This time I can draw the windows and have them resize. The issue is that if I resize smaller then what my boxes are currently set I get an error.

_curses_panel.error: move_panel() returned ERR

I am not sure if this is due to how I am checking and resizing the window or a limitation of Curses.

Can you please take a look at my code and let me know if I am losing my mind or not?

import curses
import curses.panel
screen  = curses.initscr()
curses.start_color()
curses.noecho()
curses.curs_set(1)
screen.keypad(1)
curses.cbreak()
height,width = screen.getmaxyx()

window = curses.newwin(1,1,1,1)
window2 = curses.newwin(height -2 ,(width/2)-10, 1,width/2+1)

left_panel = curses.panel.new_panel(window)
right_panel = curses.panel.new_panel(window2)

window.border('|', '|', '-', '-', '+', '+', '+', '+')
window2.border('|', '|', '-', '-', '+', '+', '+', '+')

curses.panel.update_panels()
curses.doupdate()

running = True
x = 0
while ( running  ):
    height,width = screen.getmaxyx()
    k = window.getch()
    if k == curses.KEY_RESIZE:
        window2.erase()
        window.erase()
        h, w = screen.getmaxyx()
        window2.resize(height - 2 ,(width/2)-10)
        window.resize(height - 2,(width/2) - 10)
        left_panel.replace(window)
        right_panel.replace(window2)
        left_panel.move(0,0)
        right_panel.move(0,width/2)
        window2.border('|', '|', '-', '-', '+', '+', '+', '+')
        window.border('|', '|', '-', '-', '+', '+', '+', '+')
    if k == ord('q') or x >= 10:
        running = False
        curses.endwin()
    curses.panel.update_panels()
    curses.doupdate()

This code is after I did a lot of trial and error so there may be some code that just does not make sense but all the logic for handling resizing is in this if statement.

if k == curses.KEY_RESIZE:
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
IdecEddy
  • 87
  • 1
  • 16
  • 1
    I suspect the problem is that you are calling `screen.getmaxyx()` before the call to `.getch()` that returns the resize event, and therefore you are using the old screen dimensions rather than the new ones. – jasonharper Apr 15 '18 at 01:48
  • I love you I would mark your comment as a resolution but I don't think I can do that to comments. – IdecEddy Apr 15 '18 at 02:20

1 Answers1

0

You code h, w = screen.getmaxyx() but you not use h,w variable after that. the height,width variable you use is get before getch().

So you should remove the height,width = screen.getmaxyx() and change the h, w to height, width.

And advise to use curses.wrapper(). to make Terminal back to its normal state when crash occur.

Use width//2 to support Python3.

import curses
import curses.panel

def main(screen):
    height,width = screen.getmaxyx()

    window = curses.newwin(1,1,1,1)
    window2 = curses.newwin(height -2 ,(width//2)-10, 1,width//2+1)

    left_panel = curses.panel.new_panel(window)
    right_panel = curses.panel.new_panel(window2)

    window.border('|', '|', '-', '-', '+', '+', '+', '+')
    window2.border('|', '|', '-', '-', '+', '+', '+', '+')

    curses.panel.update_panels()
    curses.doupdate()

    running = True
    x = 0
    while ( running  ):
        # height,width = screen.getmaxyx()
        k = window.getch()
        if k == curses.KEY_RESIZE:
            window2.erase()
            window.erase()
            # h, w = screen.getmaxyx()
            height,width = screen.getmaxyx()
            window2.resize(height - 2 ,(width//2)-10)
            window.resize(height - 2,(width//2) - 10)
            left_panel.replace(window)
            right_panel.replace(window2)
            left_panel.move(0,0)
            right_panel.move(0,width//2)
            window2.border('|', '|', '-', '-', '+', '+', '+', '+')
            window.border('|', '|', '-', '-', '+', '+', '+', '+')
        if k == ord('q') or x >= 10:
            running = False
            curses.endwin()
        curses.panel.update_panels()
        curses.doupdate()

curses.wrapper(main)
tinyhare
  • 2,271
  • 21
  • 25