亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號