微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在kivy中制作循环进度条?

我想用kivy和 python制作一个简单的循环进度条.我搜索了在线文档和 GitHub曲目,但没有找到解释循环进度条概念的单个正确示例.

像下面附带的图像.我想要发展.请有人帮我解决这个问题.

enter image description here

解决方法

我制作了一个小部件来表示你想要实现的目标.但是,有一些限制:

>不是仅使用.value设置进度条值,而是需要调用set_value方法.我不确定应该采取什么措施来实现与原始ProgressBar类相同的行为;
>您必须指定实现圆的大小,因为对象本身是一个椭圆.

这是代码,也有示例用法

from kivy.app import App
from kivy.uix.progressbar import ProgressBar
from kivy.core.text import Label as CoreLabel
from kivy.lang.builder import Builder
from kivy.graphics import Color,Ellipse,Rectangle
from kivy.clock import Clock


class CircularProgressBar(ProgressBar):

    def __init__(self,**kwargs):
        super(CircularProgressBar,self).__init__(**kwargs)

        # Set constant for the bar thickness
        self.thickness = 40

        # Create a direct text representation
        self.label = CoreLabel(text="0%",font_size=self.thickness)

        # Initialise the texture_size variable
        self.texture_size = None

        # Refresh the text
        self.refresh_text()

        # Redraw on innit
        self.draw()

    def draw(self):

        with self.canvas:

            # Empty canvas instructions
            self.canvas.clear()

            # Draw no-progress circle
            Color(0.26,0.26,0.26)
            Ellipse(pos=self.pos,size=self.size)

            # Draw progress circle,small hack if there is no progress (angle_end = 0 results in full progress)
            Color(1,0)
            Ellipse(pos=self.pos,size=self.size,angle_end=(0.001 if self.value_normalized == 0 else self.value_normalized*360))

            # Draw the inner circle (colour should be equal to the background)
            Color(0,0)
            Ellipse(pos=(self.pos[0] + self.thickness / 2,self.pos[1] + self.thickness / 2),size=(self.size[0] - self.thickness,self.size[1] - self.thickness))

            # Center and draw the progress text
            Color(1,1,1)
            Rectangle(texture=self.label.texture,size=self.texture_size,pos=(self.size[0]/2 - self.texture_size[0]/2,self.size[1]/2 - self.texture_size[1]/2))

    def refresh_text(self):
        # Render the label
        self.label.refresh()

        # Set the texture size each refresh
        self.texture_size = list(self.label.texture.size)

    def set_value(self,value):
        # Update the progress bar value
        self.value = value

        # Update textual value and refresh the texture
        self.label.text = str(int(self.value_normalized*100)) + "%"
        self.refresh_text()

        # Draw all the elements
        self.draw()


class Main(App):

    # Simple animation to show the circular progress bar in action
    def animate(self,dt):
        if self.root.value < 80:
            self.root.set_value(self.root.value + 1)
        else:
            self.root.set_value(0)

    # Simple layout for easy example
    def build(self):
        container = Builder.load_string(
            '''CircularProgressBar:
    size_hint: (None,None)
    height: 200
    width: 200
    max: 80''')

        # Animate the progress bar
        Clock.schedule_interval(self.animate,0.1)
        return container


if __name__ == '__main__':
    Main().run()

输出

Animated circular progress bar

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐