简体   繁体   中英

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

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, ie transparent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM