如何在 Python Kivy 中均匀分布圆圈点

问题描述 投票:0回答:0

在下面的代码中,我试图通过使圆点围绕标签的中心旋转来创建“加载动画”,并且我希望它们在顶部变慢。然而,这些点分布不均匀,当它们位于圆圈顶部时尤其值得注意。你能帮我吗?

代码:

import math

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse

Builder.load_string("""<MyLabel>:
    canvas.before:
        Color:
            rgba: 0, 0, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        size: self.parent.size
        pos: self.parent.pos
        id: circle_widget

<MyWidget>:
    FloatLayout:
        orientation: "horizontal"
        size: root.width, root.height
        size_hint: 1, 1
        MyLabel:
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
            size_hint: 0.5, 0.5""")

class MyWidget(Widget):
    pass

class CircleWidget(Widget):
    def __init__(self, **kwargs):
        super(CircleWidget, self).__init__(**kwargs)
        self.radius = 40  # Set radius  # Initialize angle
        self.angle = 0
        self.circle_diameter = 12  # Set circle diameter
        with self.canvas:
            Color(1, 0, 0)
            self.circle = Ellipse(pos=self.pos, size=(self.circle_diameter, self.circle_diameter))
        Clock.schedule_interval(self.update_circle, 0.01)

    def update_circle(self, dt):
        # print(self.angle)
        if 1.2-math.sin(math.radians(self.angle)) <= 1:
            increment_angle_coefficient = (1.2-math.sin(math.radians(self.angle)))
        else:
            increment_angle_coefficient = 1

        self.angle += dt * (360*increment_angle_coefficient)  # Increment angle over time
        x = self.center_x + self.radius * math.cos(math.radians(self.angle))  # Calculate x-coordinate
        y = self.center_y + self.radius * math.sin(math.radians(self.angle))  # Calculate y-coordinate
        # print(math.sin(math.radians(self.angle)))
        self.circle.pos = (x - self.circle_diameter / 2, y - self.circle_diameter / 2)  # Set new position

class MyLabel(Label):

    def __init__(self, **kwargs):
        super(MyLabel, self).__init__(**kwargs)
        Clock.schedule_once(self.create_dots)

    def create_dots(self, dt):
        for i in range(1, 9):
            print(dt)
            dot = CircleWidget(pos_hint={'center_x': 0.5, 'center_y': 0.5})
            dot.angle = 30*i
            self.ids.circle_widget.add_widget(dot)

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()
python python-3.x kivy kivy-language kivymd
© www.soinside.com 2019 - 2024. All rights reserved.