猿问

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

我想在做其他事情的同时在 kivy 中显示加载动画。我怎样才能做到这一点?对不起,我没有示例代码,我只是不知道从哪里开始。



人到中年有点甜
浏览 76回答 1
1回答

慕尼黑5688855

我遇到了同样的问题,您可以使用线程来实现。我不确定您要完成什么,但是假设您想在单击按钮时加载某些内容。加载时你想显示一个弹出窗口说“正在加载”。这是一个简单的示例程序,可以让您执行此操作。main.pyimport threadingimport timefrom kivy.app import Appfrom kivy.uix.popup import Popupfrom kivy.uix.label import Labelclass 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 = Trueif __name__ == '__main__':    ExampleApp().run()例子.kvScreen:    Button:        text: 'Click me'        pos_hint: {'center_x': .5, 'center_y': .5}        size_hint: .3, .2        on_release:            app.process_btn_click()希望这回答了你的问题!
随时随地看视频慕课网APP

相关分类

Python
我要回答