为了账号安全,请及时绑定邮箱和手机立即绑定

KIVY:如何同时处理多项任务

KIVY:如何同时处理多项任务

人到中年有点甜 2023-01-04 10:24:29
我想在做其他事情的同时在 kivy 中显示加载动画。我怎样才能做到这一点?对不起,我没有示例代码,我只是不知道从哪里开始。
查看完整描述

1 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

我遇到了同样的问题,您可以使用线程来实现。


我不确定您要完成什么,但是假设您想在单击按钮时加载某些内容。加载时你想显示一个弹出窗口说“正在加载”。这是一个简单的示例程序,可以让您执行此操作。


main.py


import threading

import time


from kivy.app import App

from kivy.uix.popup import Popup

from kivy.uix.label import Label



class ExampleApp(App):

    def show_popup(self):

        # Create and open a popup

        self.loading_pop = Popup(title='Please wait', 

                                 content=Label(text='Loading...'),

                                 size_hint=(.8, .5), auto_dismiss=False)

        self.loading_pop.open()


    def process_btn_click(self):

        self.show_popup() # Open the popup


        # Start a thread, this allows you to display the popup while

        #     running some long task

        my_thread = threading.Thread(target=self.some_long_task)

        my_thread.start()


    def some_long_task(self):

        current_time = time.time()

        while current_time + 3 > time.time():  # 3 seconds

            time.sleep(1)


        # When the task is done, let the popup display "Done!"

        self.loading_pop.content.text = 'Done!'

        # Also let the user click out of the popup now

        self.loading_pop.auto_dismiss = True



if __name__ == '__main__':

    ExampleApp().run()

例子.kv


Screen:

    Button:

        text: 'Click me'

        pos_hint: {'center_x': .5, 'center_y': .5}

        size_hint: .3, .2

        on_release:

            app.process_btn_click()

希望这回答了你的问题!


查看完整回答
反对 回复 2023-01-04
  • 1 回答
  • 0 关注
  • 75 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号