如何在Python3中扩展curses窗口类? [英] How to extend the curses window class in Python3?

查看:210
本文介绍了如何在Python3中扩展curses窗口类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to extend the curses built-in window class that is created by calling curses.newwin().

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

However, I am hard-pressed to find out the actual name of that class that should replace the ¿newwin? placeholder below.

#!/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一样.我还应用了全局类模式,这是讨论了一个单独的SE问题.

So I went for encapsulation rather than inheritance, which is like writing one's own API. I also applied the global class pattern, which is 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)

这篇关于如何在Python3中扩展curses窗口类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆