所以我去了封装而不是继承,这就像编写自己的 API 一样。我也应用了全局类模式 https://pythonconquerstheuniverse.wordpress.com/2010/10/20/a-globals-class-pattern-for-python/,即在一个单独的 SE 问题中讨论 https://stackoverflow.com/q/45052970/2192488.
#!/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)