Kivy:如何创建 'blocking' popup/modalview?

Kivy: How to create a 'blocking' popup/modalview?

我确实在 Whosebug 上找到了一个关于此的问题,here,但我发现它并没有回答这个问题,对我来说,Popup 和 ModalView 实际上都没有 'blocks'。我的意思是,执行是通过一个函数进行的,比如:

def create_file(self):

    modal = ModalView(title="Just a moment", size_hint=(0.5, 0.5))
    btn_ok = Button(text="Save & continue", on_press=self.save_file)
    btn_no = Button(text="Discard changes", on_press=modal.dismiss)

    box = BoxLayout()
    box.add_widget(btn_ok)
    box.add_widget(btn_no)

    modal.add_widget(box)
    modal.open()

    print "back now!"
    self.editor_main.text = ""

    new = CreateView()
    new.open()

并且 print 语句打印 "back now!" 并且函数的其余部分立即执行,尽管 ModalView 刚刚打开。我也尝试使用 Popup 而不是 ModalView,结果相同。我希望在与 Popup/ModalView 交互时暂停函数的执行。有没有办法在 kivy 中完成这项工作?我必须使用线程吗?或者我需要寻找其他解决方法吗?

您不能那样阻止,因为那样会停止事件循环并且您将无法再与您的应用进行交互。最简单的解决方法是将其拆分为两个函数,然后使用 on_dismiss 继续:

def create_file(self):

    modal = ModalView(title="Just a moment", size_hint=(0.5, 0.5))
    btn_ok = Button(text="Save & continue", on_press=self.save_file)
    btn_no = Button(text="Discard changes", on_press=modal.dismiss)

    box = BoxLayout()
    box.add_widget(btn_ok)
    box.add_widget(btn_no)

    modal.add_widget(box)
    modal.open()
    modal.bind(on_dismiss=self._continue_create_file)

def _continue_create_file(self, *args):
    print "back now!"
    self.editor_main.text = ""

    new = CreateView()
    new.open()

也可以使用 Twisted 使函数异步,尽管这有点复杂。