如何在npyscreen(Python curses包装器)中调整窗口小部件的大小?

时间:2018-09-19 21:20:03

标签: python curses npyscreen

我希望npyscreen应用程序中某些小部件的高度随着终端的大小而改变。

具体来说,我希望这两个列窗口小部件在调整终端大小时继续占据终端高度的大约相同部分,而行小部件在底部时,将其保留在终端的底部,当终端为调整大小。

我已经完成了一些工作,但是当终端调整大小时,整体渲染出现了问题,例如小部件不知道我要在哪里绘制它们。可能出什么问题了?该如何正确完成?

import curses
import sys

import npyscreen
npyscreen.disableColor()

def main():
    app = App()
    app.run()

class App(npyscreen.NPSApp):
    def main(self):
        form = npyscreen.FormBaseNew(name = "COMMUNICATIONS")
        column_height = terminal_dimensions()[0] - 9
        widget_contacts = form.add(
            Column,
            name       = "CONTACTS",
            relx       = 2,
            rely       = 2,
            max_width  = 20,
            max_height = column_height
        )
        widget_messages = form.add(
            Column,
            name       = "MESSAGES",
            relx       = 23,
            rely       = 2,
            max_height = column_height
        )
        widget_input = form.add(
            npyscreen.BoxTitle,
            name       = "INPUT",
            max_height = 5
        )
        widget_input.resize
        widget_contacts.values  = ["alice", "bob"]
        widget_messages.values  = ["2018-09-19T2001Z    a message", "2018-09-19T2002Z    another message"]
        widget_input.values     = ["sup"]
        #exit_button = form.add(
        #    ExitButton,
        #    name       = "EXIT",
        #    #relx       = 78,
        #    rely       = 23
        #)
        widget_contacts.max_height = 5
        form.edit()

class Column(npyscreen.BoxTitle):
    def resize(self):
        self.max_height = int(0.73 * terminal_dimensions()[0])

class ExitButton(npyscreen.ButtonPress):
    def whenPressed(self):
        sys.exit(0)

def terminal_dimensions():
    return curses.initscr().getmaxyx()

if __name__ == "__main__":
    main()

0 个答案:

没有答案