自动关闭kivy中的弹出窗口

Posted

技术标签:

【中文标题】自动关闭kivy中的弹出窗口【英文标题】:Auto dismiss a popup in kivy 【发布时间】:2018-02-13 01:38:06 【问题描述】:

我需要在完成另一个类的函数后关闭弹出窗口,或者至少在特定时间后(例如(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】:

您必须添加一个函数来关闭 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()

输出

【讨论】:

以上是关于自动关闭kivy中的弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章

Kivy Python 中的弹出窗口 - 显示在前面

kivy:带有进度条的弹出窗口

更改 Kivy 中的弹出文本

Kivy:弹出窗口只能有一个小部件作为内容(当我导入两个不同的弹出模块时)

Kivy:访问不同类的方法

Kivy 如何创建一个可根据内容调整大小的弹出窗口?