如何使用npyscreen切换当前显示的表单

2024-04-26 01:03:06 发布

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

我正在使用npyscreen构建一个简单的应用程序来构建精美的菜单。正如文档所说,我使用self.parentApp检索的switchForm()方法来更改当前显示的表单。但是,在splash.show_splash()中调用时,不会发生任何事情(应用程序退出,因为setNextFormNone

如何正确地切换表单

源代码:

class splash(npyscreen.Form):
    def afterEditing(self):
        self.parentApp.switchFormPrevious()
    def create(self):
        self.new_splash = self.add(npyscreen.Pager, values=BANNER)

class whatDo(npyscreen.FormBaseNewWithMenus):
    def afterEditing(self):
        self.parentApp.setNextForm(None)

    def create(self):
        self.add(npyscreen.TitlePager, name="Hello")
        self.m1 = self.new_menu(name="File", shortcut=None)
        self.m1.addItemsFromList([
            ("M1-A", self.open),
            ("M1-B",   self.new),
            ("M1-C", self.exit_application),
        ])
        self.m2 = self.new_menu(name="Edit", shortcut=None)
        self.m2.addItemsFromList([
            ("M2-A", self.open),
            ("M2-B",   self.new),
        ])
        self.m3 = self.new_menu(name="Other", shortcut=None)
        self.m3.addItemsFromList([
            ("M3-A", self.open),
            ("M3-A",   self.show_splash)
        ])
    def show_splash(self):
        self.parentApp.switchForm('SPLASH')

    def exit_application(self):
        self.parentApp.switchForm(None)  


    def open(self):
        pass
    def new(self):
        pass

class MyApplication(npyscreen.NPSAppManaged):
    def onStart(self):
        self.addForm('MAIN', whatDo, name='Main Menu')
        self.addForm('SPLASH', splash, lines=30, name='Splash Form')
if __name__ == '__main__':
   TestApp = MyApplication().run()

Tags: nameselfnonenewdefshowopenshortcut
1条回答
网友
1楼 · 发布于 2024-04-26 01:03:06

实际上,我通过以下方法解决了这个问题:

class Form1(npyscreen.Form):
    def create(self):
        self.name = "I'm the first form! I am so happy!"

    def afterEditing(self):
        self.parentApp.setNextForm("second")

class Form2(npyscreen.Form):
    def create(self):
        self.name = "Im the second form!"

    def afterEditing(self):
        self.parentApp.setNextForm("MAIN")

class App(npyscreen.NPSAppManaged):
    def onStart(self):
        self.registerForm("MAIN", Form1())
        self.registerForm("second", Form2())

这样,您可以通过单击众所周知的“确定”按钮在这两个表单之间切换。我的意思是,这对我很有用

编辑#1: 很抱歉,我在你提出要求10个月后回答了你,但我刚刚开始使用npyscreen

编辑#2: 我必须说,npyscreen是使用UI创建python控制台应用程序的一种非常好的方法。我试着自己做一些类似的事情,回到我痴迷于Python的时候,但是你可能猜到了它是如何进行的

相关问题 更多 >

    热门问题