【问题标题】:Kivy: How to create a 'blocking' popup/modalview?Kivy:如何创建“阻塞”弹出窗口/模态视图?
【发布时间】:2015-04-16 00:25:56
【问题描述】:

我确实在 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 刚刚打开。我也尝试使用 Popup 而不是 ModalView,结果相同。我希望在与 Popup/ModalView 交互时暂停函数的执行。有没有办法在kivy中做到这一点?我必须使用线程吗?还是我需要找到其他解决方法?

【问题讨论】:

    标签: python python-2.7 kivy


    【解决方案1】:

    您不能这样阻止,因为这会停止事件循环,并且您将无法再与您的应用进行交互。最简单的解决方法是将其拆分为两个函数,然后使用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 使函数异步,虽然这有点复杂。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多