我想扩展通过调用 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/