如何在Python3中扩展curses窗口类?

时间:2017-07-12 07:16:04

标签: python-3.x class extend curses python-curses

我想扩展通过调用curses创建的curses.newwin()内置窗口类。

但是,我很难找到应该替换下面¿newwin?占位符的该类的实际名称。

#!/usr/bin/python3

import curses

class Window(curses.¿newwin?):

    def __init__(self, title, h, w, y, x):
        super().__init__(h, w, y, x)
        self.box()
        self.hline(2, 1, curses.ACS_HLINE, w-2)
        self.addstr(1, 2, title)
        self.refresh()

def main(screen):
    top_win = Window('Top window', 6, 32, 3, 6)
    top_win.addstr(3, 2, 'Test string added.')
    top_win.refresh()
    ch = top_win.getch()

# MAIN
curses.wrapper(main)

1 个答案:

答案 0 :(得分:1)

所以我选择封装而不是继承,就像编写自己的API一样。我还应用了global class patterndiscussed in a separate SE question

#!/usr/bin/python3

import curses

class win:
    pass

class Window:

    def __init__(self, title, h, w, y, x):
        self.window = curses.newwin(h, w, y, x)
        self.window.box()
        self.window.hline(2, 1, curses.ACS_HLINE, w-2)
        self.window.addstr(1, 2, title)
        self.window.refresh()

    def clear(self):
        for y in range(3, self.window.getmaxyx()[0]-1):
            self.window.move(y,2)
            self.window.clrtoeol()
        self.window.box()

    def addstr(self, y, x, string, attr=0):
        self.window.addstr(y, x, string, attr)

    def refresh(self):
        self.window.refresh()

def main(screen):
    win.top = Window('Top window', 6, 32, 3, 6)
    win.top.addstr(3, 2, 'Test string added.')
    win.top.refresh()
    ch = win.top.getch()

# MAIN
curses.wrapper(main)