1 回答

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()
希望这回答了你的问题!
添加回答
举报