当进程在后台运行时弹出Kivy显示

时间:2019-02-28 10:59:11

标签: python popup kivy

在我的kivy项目中,我有一个按钮可以让我生成.png格式的matplotlib图。生成此图像需要花费时间(大约20秒),我想显示一个弹出窗口来警告用户。

我尝试了什么:

<MyPopup@Popup>:
    auto_dismiss: False
    Button:
        text: 'This could take time, please wait :)  '
        on_release: root.dismiss()

和:

ActionButton:
                        text: 'generate graph'
                        on_release: Factory.MyPopup().open()
                        #on_release: root.generate_graph() 

不幸的是,如果我取消注释第二个“ on_release”,则pop_up窗口将永远不会出现?

您有任何猜测吗?

先谢谢您!

2 个答案:

答案 0 :(得分:1)

要在方法运行时显示弹出窗口,请使用线程。当弹出窗口启动时,该方法在线程中运行。

代码:

def popupThread(self):          
    
    #set the popup structure
    self.popup = ActivityBox(self)
    self.popup.open()
    
    # run the method in threads
    t1 = threading.Thread(target = self.someMethod)
    t1.start()

弹出窗口在Builder.load_string()中定义:

def build(self):
        sm = Builder.load_string("""    
<ActivityBox>:
    size_hint: 1, .7
    auto_dismiss: False
    title: 'some activity' 
    title_align: "center"
    title_size: 30
    BoxLayout:
        orientation: "vertical"
        Label:
            font_size: '30sp'
            text: 'work in progress'
        BoxLayout:
            orientation: "horizontal"
            spacing: 10
            size_hint: 1, .5
""")

答案 1 :(得分:0)

您正在覆盖on_release方法。

ActionButton:
    text: 'generate graph'
    on_release: 
        Factory.MyPopup().open()
        root.generate_graph()