KIVY:如何同时处理多项任务
KIVY: How to multitask
我想在执行其他操作时在 kivy 中显示加载动画。我怎样才能做到这一点?对不起,我没有示例代码,我只是不知道从哪里开始。
我遇到了同样的问题,你可以使用线程来解决这个问题。
我不确定您想要完成什么,但假设您想在单击按钮时加载一些内容。加载时你想显示一个弹出窗口说 "loading"。这是一个简单的示例程序,可以让您执行此操作。
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()
example.kv
Screen:
Button:
text: 'Click me'
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: .3, .2
on_release:
app.process_btn_click()
希望这能回答您的问题!
我想在执行其他操作时在 kivy 中显示加载动画。我怎样才能做到这一点?对不起,我没有示例代码,我只是不知道从哪里开始。
我遇到了同样的问题,你可以使用线程来解决这个问题。
我不确定您想要完成什么,但假设您想在单击按钮时加载一些内容。加载时你想显示一个弹出窗口说 "loading"。这是一个简单的示例程序,可以让您执行此操作。
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()
example.kv
Screen:
Button:
text: 'Click me'
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: .3, .2
on_release:
app.process_btn_click()
希望这能回答您的问题!