python-3.x - 如何扩展Python3中的curses窗口类?

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

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

但是,我很难找出应该替换下面的 ¿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)

最佳答案

所以我选择了封装而不是继承,这就像编写自己的 API。我还应用了 global class pattern ,即 discussed 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)

关于python-3.x - 如何扩展Python3中的curses窗口类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050864/

相关文章:

python - 如何使用Python3显示相对于字长的字数(串行,即以有序的方式)?

ios - 使用 Codable 从 2 个不同的 JSON 文件实例化单个类,而不使用选项

c++ - C++中的Bignum除法和赋值

php - 如何向 PHP 中的现有类添加方法?

Java扩展问题

python - 使用 python redisearch 客户端按标签过滤搜索

python - 如何在布局中间插入 QWidgets?

python - 如何在 Python 3.7+ 中注入(inject)类变量注解?

python - 使用元类动态扩展 django 模型

python - 'r = yield n' 和 'r = (yield n)' 有什么区别?