Kivy:如何创建“阻止"弹出/模态视图? [英] Kivy: How to create a 'blocking' popup/modalview?

查看:98
本文介绍了Kivy:如何创建“阻止"弹出/模态视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在stackoverflow上找到了有关此问题,请在此处 ,但我发现它只是无法回答问题,对我而言,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'. 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()

print语句打印立即返回!"即使ModalView刚刚打开,该函数的其余部分也会立即执行.我也使用弹出窗口而不是ModalView进行了尝试,结果相同.我想在与Popup/ModalView交互时暂停该函数的执行.是否有办法将其内置到kivy中?我必须使用线程吗?还是我需要找到其他解决方法?

And the print statement prints "back now!" and the rest of the function is immediately executed, despite the fact the ModalView just opened. I also tried this using a Popup instead of a ModalView, with the same result. I would like execution in the function to pause while I interact with the Popup/ModalView. Is there a way for this to be done built into kivy? Must I use threads? Or will I need to just find some other workaround?

推荐答案

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

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:

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使函数异步,尽管这要复杂一些.

It's also possible to use Twisted to make the function asynchronous, though that's a little more complicated.

这篇关于Kivy:如何创建“阻止"弹出/模态视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆