简体   繁体   English

Kivy:如何创建“阻止”弹出窗口/模态视图?

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

I did find a question on this on stackoverflow, here , but I find it just doesn't answer the question, as for me, neither the Popup nor the ModalView actually 'blocks'. 我没有找到计算器,这个问题在这里 ,但我觉得它只是不回答这个问题,因为对我来说,无论是弹出窗口还是ModalView其实“块”。 What I mean is, execution is moving through a function, like: 我的意思是,执行正在遍历一个函数,例如:

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

And the print statement prints "back now!" 并且打印语句打印“立即返回!” and the rest of the function is immediately executed, despite the fact the ModalView just opened. 即使ModalView刚刚打开,该函数的其余部分也会立即执行。 I also tried this using a Popup instead of a ModalView, with the same result. 我也使用弹出窗口而不是ModalView进行了尝试,结果相同。 I would like execution in the function to pause while I interact with the Popup/ModalView. 在与Popup / ModalView交互时,我希望函数中的执行暂停。 Is there a way for this to be done built into kivy? 是否有办法将其内置到kivy中? Must I use threads? 我必须使用线程吗? Or will I need to just find some other workaround? 还是我需要找到其他解决方法?

You can't block like that, as that would stop the event loop and you wouldn't be able to interact with your app anymore. 您不能那样阻止,因为那样会停止事件循环,并且您将无法再与您的应用进行交互。 The easiest fix is to split this into two functions, and use on_dismiss to continue: 最简单的解决方法是将其拆分为两个函数,并使用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()

It's also possible to use Twisted to make the function asynchronous, though that's a little more complicated. 也可以使用Twisted使函数异步,尽管这要复杂一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM