簡體   English   中英

Kivy:如何創建“阻止”彈出窗口/模態視圖?

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

我沒有找到計算器,這個問題在這里 ,但我覺得它只是不回答這個問題,因為對我來說,無論是彈出窗口還是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使函數異步,盡管這要復雜一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM