我确实在stackoverflow上找到了一个问题,here,但我发现它并没有回答问题,就我而言,Popup或ModalView均未“阻塞”。我的意思是,执行正在遍历一个函数,例如:

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()


然后打印语句打印“立即返回!”即使ModalView刚刚打开,该函数的其余部分也会立即执行。我也使用弹出窗口而不是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使函数异步,尽管这要复杂一些。

关于python - Kivy:如何创建“阻止”弹出/模态视图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29663272/

10-12 00:38