问题
I would like to extend the curses
built-in window class that is created by calling curses.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)
回答1:
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)
来源:https://stackoverflow.com/questions/45050864/how-to-extend-the-curses-window-class-in-python3