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

2024-06-24 13:07:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的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()

Tags: nameimportselfformadd终端main部件