自动关闭kivy中的弹出窗口

时间:2017-09-04 20:04:37

标签: python class popup kivy

我需要在另一个类中完成一个函数后关闭弹出窗口,或者至少在特定时间之后(3秒) 弹出显示加载gif图像 通知用户等待操作功能

loading image

******* ******蟒

from kivy.app import App 
from kivy.uix.popup import Popup 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen

class ScreenManagement(ScreenManager):
    pass

class progress(Popup):  
    pass 
class Func_(Screen):
# function
    pass

presentation = Builder.load_file("Try_.kv") 
class MainApp(App):

    def build(self):
        return presentation

if __name__ == "__main__":
    MainApp().run()

******** KV文件*********

#:import Factory kivy.factory.Factory

<Popup>:
    separator_color: 1, 1, 1, 1
    background: "White.png"
    Button:
        id: btn
        disabled: True
        background_disabled_normal: "White.png"
        text: "Hello"
        Image:
            source: "Loading.gif"
            size: root.size

ScreenManagement:
    PopupBox:

<PopupBox>:
    BoxLayout:
        Button:
            text: "Click"
            on_release:
                Factory.Popup().open()

1 个答案:

答案 0 :(得分:1)

您必须添加一个函数来关闭Popup消息并使用Clock.schedule_once来调用该函数。有关详细信息,请参阅以下示例。

main.py

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock


class ScreenManagement(ScreenManager):
    pass


class Progress(Popup):

    def __init__(self, **kwargs):
        super(Progress, self).__init__(**kwargs)
        # call dismiss_popup in 2 seconds
        Clock.schedule_once(self.dismiss_popup, 2)

    def dismiss_popup(self, dt):
        self.dismiss()


class Func(Screen):
    # function
    pass


class MainApp(App):

    def build(self):
        return ScreenManagement()

if __name__ == "__main__":
    MainApp().run()

main.kv

#:import Factory kivy.factory.Factory
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

<Progress>:
    separator_color: 1, 1, 1, 1
    background: "White.png"
    Button:
        id: btn
        disabled: True
        background_disabled_normal: "White.png"
        text: "Hello"
        Image:
            source: "Loading.gif"
            size: root.size

<ScreenManagement>:
    transition: FadeTransition()
    Func:

<Func>:
    BoxLayout:
        Button:
            text: "Click"
            on_release:
                Factory.Progress().open()

输出

enter image description here