我想要的基本上是一个常规的 npyscreen.Form,但我想要“确定”按钮说“退出”。
看来你不能在常规 npyscreen.Form 中更改按钮的名称,所以我尝试子类化 npyscreen.ButtonPress:
import npyscreen
class ExitButton(npyscreen.ButtonPress):
def whenPressed(self):
self.parentApp.setNextForm(None)
class MainForm(npyscreen.FormBaseNew):
def create(self):
self.exitButton = self.add(ExitButton, name="Exit", relx=-12, rely=-3)
class App(npyscreen.NPSAppManaged):
def onStart(self):
self.addForm("MAIN", MainForm, name="My Form")
if __name__ == "__main__":
app = App().run()
按钮出现了,但是当你点击它时,你会得到 'ExitButton' object has no attribute 'parentApp'
。
有更简单的方法吗?
最佳答案
Edwin 的权利,使用 self.parent.parentApp
而不是 self.parentApp
。
要退出应用程序,请使用 switchForm(None)
而不是 setNextForm(None)
。
def whenPressed(self):
self.parent.parentApp.switchForm(None)
引用:一个post by npyscreen's author确认这按预期工作。
关于python - 如何在 npyscreen 中制作退出按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41515278/