1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
我遇到了同樣的問題,您可以使用線程來實(shí)現(xiàn)。
我不確定您要完成什么,但是假設(shè)您想在單擊按鈕時(shí)加載某些內(nèi)容。加載時(shí)你想顯示一個(gè)彈出窗口說“正在加載”。這是一個(gè)簡單的示例程序,可以讓您執(zhí)行此操作。
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()
希望這回答了你的問題!
添加回答
舉報(bào)