Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python curses terminal settings changed

I'm pretty new to curses but I wrote a working little curses application. But after a while I noticed that my default terminal settings were changed during the session. The background color is a solid black, but I've configured a transparent terminal. Also the color looks more like white than grey.

My code, but I'm sure it's not related to the problem. I'm using debian wheezy with python 2.7.2

#!/usr/bin/env python

import curses

class Monitor:
    def __init__(self, screen):
        self.screen = screen
        self.height, self.width = self.screen.getmaxyx()
        self.screen.nodelay(1)

    def redraw(self):
        self.screen.clear()
        self.screen.addstr(1, 1, 'hai')
        self.screen.refresh()

    def main(self):
        while 1:
            key = self.screen.getch()
            if key == ord('q'): break
            self.redraw()

def main(stdscr):
    mon = Monitor(stdscr)
    mon.main()

if __name__ == '__main__':
    try:
        curses.wrapper(main)
    except KeyboardInterrupt:
        pass
like image 289
sfx Avatar asked Dec 05 '25 18:12

sfx


1 Answers

I figured I should answer this, since I've been looking for this before.

In main(), you need to add

curses.use_default_colors()

This will use your terminal's colors instead of curses' overwriting them. This means that the background color will be transparent if no background color is set.

If, later, you want to create a color pair with a transparent background, instead of for example

curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)

use

curses.init_pair(1, curses.WHITE, -1)

This will use the default background, i.e. transparent.

like image 72
Edo Mangelaars Avatar answered Dec 08 '25 10:12

Edo Mangelaars



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!