python – 如何组合多个TUI表单来编写更复杂的应用程序?

前端之家收集整理的这篇文章主要介绍了python – 如何组合多个TUI表单来编写更复杂的应用程序?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想编写一个带有基于文本的用户界面(TUI)的程序,该程序由几种形式组成.

Presentation of the several forms.

>第一个表单包含“列表”.每个列表元素代表一个按钮.
>如果按下相应的按钮,则应显示另一个表单,其中可以输入列表条目的数据.
>然后再次显示第一个表单(使用更新的列表条目).

这是我的尝试,它使用库npyscreen但不返回第一个表单.代码也不包含更改列表项的逻辑.

    #! /usr/bin/env python3
    # coding:utf8

    import npyscreen

    # content
    headers = ["column 1","column 2","column 3","column 4"]
    entries = [["a1","a2","a3","a4"],["b1","b2","b3","b4"],["c1","c2","c3","c4"],["d1","d2","d3","d4"],["e1","e2","e3","e4"]]


    # returns a string in which the segments are padded with spaces.
    def format_entry(entry):
        return "{:10} | {:10} | {:10} | {:10}".format(entry[0],entry[1],entry[2],entry[3])


    class SecondForm(npyscreen.Form):
        def on_ok(self):
            self.parentApp.switchFormPrevIoUs()

        # add the widgets of the second form
        def create(self):
            self.col1 = self.add(npyscreen.TitleText,name="column 1:")
            self.col2 = self.add(npyscreen.TitleText,name="column 2:")
            self.col3 = self.add(npyscreen.TitleText,name="column 3:")
            self.col4 = self.add(npyscreen.TitleText,name="column 4:")


    class MainForm(npyscreen.Form):    
        def on_ok(self):
            self.parentApp.switchForm(None)

        def changeToSecondForm(self):
            self.parentApp.change_form("SECOND")

        # add the widgets of the main form
        def create(self):
            self.add(npyscreen.FixedText,value=format_entry(headers),editable=False,name="header")

            for i,entry in enumerate(entries):
                self.add(npyscreen.ButtonPress,when_pressed_function=self.changeToSecondForm,name=format_entry(entry))


    class TestTUI(npyscreen.NPSAppManaged):
        def onStart(self):
            self.addForm("MAIN",MainForm)
            self.addForm("SECOND",SecondForm,name="Edit row")

        def onCleanExit(self):
            npyscreen.notify_wait("Goodbye!")

        def change_form(self,name):
            self.switchForm(name)


    if __name__ == "__main__":
        tui = TestTUI()
        tui.run()
@H_404_18@最佳答案
接下来是我对这个问题的看法,可以将其描述为控制台master-detail user interface的实现.

这使用urwid library,构建一些custom widgets以实现所描述的UI,其具有两种模式:主视图(其中主要小部件是一堆记录)和详细视图(覆盖的对话框,后面有主视图).

有许多事情可以改进,包括让它看起来更漂亮. 原文链接:https://www.f2er.com/python/438914.html

猜你在找的Python相关文章