在Kivy建立一个简单的进度条或加载动画 [英] Building a simple progress bar or loading animation in Kivy

查看:1002
本文介绍了在Kivy建立一个简单的进度条或加载动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我开发的cmd线路实用程序编写一个Kivy UI。一切都可以正常工作,但是某些进程可能需要几秒到几分钟的时间才能处理,我想向用户提供一些流程正在运行的指示。理想情况下,这将是一个旋转轮或装载栏或某物的形式,但即使我可以更新我的显示屏来向用户显示一个进程正在运行,这将比现在更好。



目前,用户按主UI中的按钮。这将弹出一个用户验证一些关键信息的弹出窗口,如果他们对这些选项感到满意,他们会按运行按钮。我已经尝试打开一个新的弹出窗口来告诉他们该进程正在运行,但是由于显示在进程完成之前不会更新,所以这不起作用。



我的编码经验已经丧失,但主要是在数学和工程的上下文中,所以我对UI的设计非常新颖,必须处理事件和线程。

解决方案

我最近正在解决您所描述的问题:显示不会更新,直到进程完成



这是一个完整的例子,我在#Kivy中的@andy_s的帮助下工作IRC频道:



我的main.py:

 来自kivy。来自kivy.uix.popup的应用程序导入App 
来自kivy.factory import的Popup
来自kivy.properties的工厂
importProperty
来自kivy.clock import Clock

导入时间,线程

类PopupBox(Popup):
pop_up_text = ObjectProperty()
def update_pop_up_text(self,p_message):
self.pop_up_text。 text = p_message

class ExampleApp(App):
def show_popup(self):
self.pop_up = Factory.PopupBox()
self.pop_up.update_pop_up_text '运行一些任务...')
self.pop_up.op en()

def process_button_click(self):
#打开弹出窗口
self.show_popup()

#调用一些可能需要的方法一会儿跑。
#我使用线程来模拟这个
mythread = threading.Thread(target = self.something_that_takes_5_seconds_to_run)
mythread.start()

def something_that_takes_5_seconds_to_run self):
thistime = time.time()
,而thistime + 5> time.time():#5秒
time.sleep(1)

#一旦长时间运行的任务完成,关闭弹出窗口。
self.pop_up.dismiss()

如果__name__ ==__main__:
ExampleApp()。run()
/ pre>

我的example.kv:

  AnchorLayout:
anchor_x:'center'
anchor_y:'center'
按钮:
height:40
width:100
size_hint:(无,无)
文本:'点击我'
on_press:app.process_button_click()

< PopupBox> ;:
pop_up_text:_pop_up_text
size_hint:.5,.5
auto_dismiss:True
标题:'状态'

BoxLayout:
orientation:vertical
标签:
id:_pop_up_text
文本:''

如果运行此示例,您可以单击点击我按钮,它应该以模态/弹出窗口的形式打开一个进度条。这个弹出窗口将保持打开5秒钟,而不会阻塞主窗口。 5秒钟后,弹出窗口将自动关闭。


I am writing a Kivy UI for cmd line utility I have developed. Everything works fine, but some of the processes can take from a few seconds to a few minutes to process and I would like to provide some indication to the user that the process is running. Ideally this would be in the form of a spinning wheel or loading bar or something, but even if I could update my display to show the user that a process is running, it would be better than what I have now.

Currently, the user presses a button in the main UI. This brings up a popup that verifies some key information with the user, and if they are happy with those options, they press a 'run' button. I have tried opening a new popup to tell them that the process is running, but because the display doesn't update until the process finishes, this doesn't work.

I have a lost of coding experience, but mostly in the context of math and engineering, so I am very new to the designing of UIs, and having to handle events and threads. A simple self contained example would be greatly appreciated.

解决方案

I was recently tackling the problem you described: display doesn't update until the process finishes

Here is a complete example that I got working with the help of @andy_s in the #Kivy IRC channel:

My main.py:

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.clock import Clock

import time, threading

class PopupBox(Popup):
    pop_up_text = ObjectProperty()
    def update_pop_up_text(self, p_message):
        self.pop_up_text.text = p_message

class ExampleApp(App):
    def show_popup(self):
        self.pop_up = Factory.PopupBox()
        self.pop_up.update_pop_up_text('Running some task...')
        self.pop_up.open()

    def process_button_click(self):
        # Open the pop up
        self.show_popup()

        # Call some method that may take a while to run.
        # I'm using a thread to simulate this
        mythread = threading.Thread(target=self.something_that_takes_5_seconds_to_run)
        mythread.start()

    def something_that_takes_5_seconds_to_run(self):
        thistime = time.time() 
        while thistime + 5 > time.time(): # 5 seconds
            time.sleep(1)

        # Once the long running task is done, close the pop up.
        self.pop_up.dismiss()

if __name__ == "__main__":
    ExampleApp().run()

My example.kv:

AnchorLayout:
    anchor_x: 'center'
    anchor_y: 'center'
    Button:
        height: 40
        width: 100
        size_hint: (None, None)
        text: 'Click Me'
        on_press: app.process_button_click()

<PopupBox>:
    pop_up_text: _pop_up_text
    size_hint: .5, .5
    auto_dismiss: True
    title: 'Status'   

    BoxLayout:
        orientation: "vertical"
        Label:
            id: _pop_up_text
            text: ''

If you run this example, you can click the Click Me button, which should open up a "progress bar" in the form of a modal/pop-up. This pop up will remain open for 5 seconds without blocking the main window. After 5 seconds, the pop up will automatically be dismissed.

这篇关于在Kivy建立一个简单的进度条或加载动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆