Carlo
Carlo

Reputation: 1578

Python curses, splitting terminal window in 4 pads - prefresh() returned ERR

I'm running a multiprocessing system in Python, and I was planning to use curses to divide the terminal window in 4 quadrants, and display the output of each of the processes in one of them.

So, the final output should look something like:

--------------------------------
|               |               |
|   PROCESS01   |   PROCESS02   |
|               |               |
---------------------------------
|               |               |
|   PROCESS03   |   PROCESS04   |
|               |               |
---------------------------------

So far, I tried splitting the window in 4 pads, like this:


def main():

    screen = curses.initscr()
    cols_tot = curses.COLS
    rows_tot = curses.LINES
    cols_mid = int(0.5*cols_tot)   ## middle point of the window
    rows_mid = int(0.5*rows_tot)

    pad11 = curses.newpad(rows_mid, cols_mid)
    pad12 = curses.newpad(rows_mid, cols_mid)
    pad21 = curses.newpad(rows_mid, cols_mid)
    pad22 = curses.newpad(rows_mid, cols_mid)
    pad11.addstr(0, 0, "*** PROCESS 01 ***")
    pad12.addstr(0, 0, "*** PROCESS 02 ***")
    pad21.addstr(0, 0, "*** PROCESS 03 ***")
    pad22.addstr(0, 0, "*** PROCESS 04 ***")
    pad11.refresh(0,0, 0,0, rows_mid,cols_mid)
    pad12.refresh(0,cols_mid, 0,cols_mid, rows_mid,cols_tot-1)
    pad21.refresh(rows_mid,0, rows_mid,0, cols_tot-1,rows_mid)
    pad22.refresh(rows_mid, cols_mid, rows_mid,cols_mid, rows_tot-1,cols_tot-1)


    curses.napms(3000)
    curses.endwin()


if __name__ == '__main__':
    main()

but I'm getting the error:

File "screen_show.py", line 78, in <module> main()
                                                                                                          File "screen_show.py", line 46, in main
                                                                                                                                                     pad12.refresh(0,cols_mid, 0,cols_mid, rows_mid,cols_tot-1)
_curses.error: prefresh() returned ERR

Upvotes: 6

Views: 2495

Answers (1)

xitong
xitong

Reputation: 349

The lines should be changed to

pad11.refresh(0,0, 0,0, rows_mid,cols_mid)
pad12.refresh(0,0, 0,cols_mid, rows_mid,cols_tot-1)
pad21.refresh(0,0, rows_mid,0, cols_tot-1,rows_mid)
pad22.refresh(0,0, rows_mid,cols_mid, rows_tot-1,cols_tot-1)

result: final result

Full working code

import curses


def draw_menu(stdscr):
    k = 0
    # Clear and refresh the screen for a blank canvas
    stdscr.clear()
    stdscr.refresh()
    while (k != ord('q')):
        # Initialization
        height, width = stdscr.getmaxyx()

        cols_tot = width
        rows_tot = height
        cols_mid = int(0.5*cols_tot)  # middle point of the window
        rows_mid = int(0.5*rows_tot)

        pad11 = curses.newpad(rows_mid, cols_mid)
        pad12 = curses.newpad(rows_mid, cols_mid)
        pad21 = curses.newpad(rows_mid, cols_mid)
        pad22 = curses.newpad(rows_mid, cols_mid)
        pad11.addstr(0, 0, "*** PROCESS 01 ***")
        pad12.addstr(0, 0, "*** PROCESS 02 ***")
        pad21.addstr(0, 0, "*** PROCESS 03 ***")
        pad22.addstr(0, 0, "*** PROCESS 04 ***")
        pad11.refresh(0, 0, 0, 0, rows_mid, cols_mid)
        pad12.refresh(0, 0, 0, cols_mid, rows_mid, cols_tot-1)
        pad21.refresh(0, 0, rows_mid, 0, cols_tot-1, rows_mid)
        pad22.refresh(0, 0, rows_mid, cols_mid, rows_tot-1, cols_tot-1)

        k = stdscr.getch()


def main():
    curses.wrapper(draw_menu)


if __name__ == "__main__":
    main()

Upvotes: 3

Related Questions