首页 > 解决方案 > 如何在kivy中旋转不同角度的标签

问题描述

我正在研究“命运之轮”之类的东西。我有一个我想在轮子中看到的文本列表。

这个轮子工作正常,但由于角度问题,我无法列出我的标签列表。

我根本无法将具有相同来源的不同角度的标签放在一类中。

我可以调整类(布局)角度,但这是一次性的,我需要将它绑定到“for”语句。

我找到了一个解决方案,但还不够。对于每个角度,我创建了一个不同的类,并将它们放在类的顶部。

问题是列表的来源可能会改变,它也必须绑定到“for”语句。

这是 main.py :

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import PushMatrix, PopMatrix, Rotate
import math 

am0=["Example1","Example2","Example3"] #HERE IS MY LIST

class MainWidget(Widget):
    pass


# ---------THIS PART IS FOR TURNING WHEEL ---------------------------------------------:
class Cark(FloatLayout):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):

        y = (touch.y - self.center[1])
        x = (touch.x - self.center[0])
        calc = math.degrees(math.atan2(y, x))
        self.angle1 = calc if calc > 0 else 360+calc
        self.mvmt = self.angle

        return super(Cark, self).on_touch_down(touch) 

    def on_touch_move(self, touch):
        y = (touch.y - self.center[1])
        x = (touch.x - self.center[0])
        calc = math.degrees(math.atan2(y, x))
        angle2 = calc if calc > 0 else 360+calc

        self.angle = self.mvmt + (angle2-self.angle1)%360

#----------------------------------------------------------------------------------#

#AFTER THAT POINT I HAVE CREATE 3 DIFFERENT CLASS IN ORDER TO ADJUST DIFFERENT ANGLES;

class DayaI(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        b1=Label(text=str("Example1"), pos_hint={'center_x': .8, 'center_y': .5},size_hint = (None, None))
        self.add_widget(b1)          
        with self.canvas.before:
            PushMatrix()
            self.rotation = Rotate()
            self.rotation.origin=self.center
            self.rotation.angle=45 # THIS ANGLE ADJUST LAYOUTS ANGLE
            self.bind(center=lambda _, value: setattr(self.rotation, "origin", value))            
        with self.canvas.after:
            PopMatrix()

class DayaII(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        b1=Label(text=str("Example2"), pos_hint={'center_x': .8, 'center_y': .5},size_hint = (None, None))
        self.add_widget(b1)          
        with self.canvas.before:
            PushMatrix()
            self.rotation = Rotate()
            self.rotation.origin=self.center
            self.rotation.angle=0 # THIS ANGLE ADJUST LAYOUTS ANGLE
            self.bind(center=lambda _, value: setattr(self.rotation, "origin", value))  
        with self.canvas.after:
            PopMatrix()

class DayaIII(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        b1=Label(text=str("Example3"), pos_hint={'center_x': .8, 'center_y': .5},size_hint = (None, None))
        self.add_widget(b1)          
        with self.canvas.before:
            PushMatrix()
            self.rotation = Rotate()
            self.rotation.origin=self.center
            self.rotation.angle=90 # THIS ANGLE ADJUST LAYOUTS ANGLE
            self.bind(center=lambda _, value: setattr(self.rotation, "origin", value))  
        with self.canvas.after:
            PopMatrix()


#----------------------------------------------------------------------------------#

# THE LOGIC I WANT WHICH IS NOT WORKING

class Day(FloatLayout): 
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        for i in am0:
            b1=Label(text=str(i), pos_hint={'center_x': .8, 'center_y': .5},size_hint = (None, None))
            self.add_widget(b1)          
            with self.canvas.before: # THIS PART DOESNT WORK BECAUSE IT ROTATES THE WHOLE WODGET
                PushMatrix()
                self.rotation = Rotate()
                self.rotation.origin=self.center
                self.rotation.angle=(360/len(am0)) #I WANT TO ADJUST ANGLE IN ORDER TO LENGHT OF MY LIST
                self.bind(center=lambda _, value: setattr(self.rotation, "origin", value))  
            with self.canvas.after:
                PopMatrix()


class MyyApp(App):
    pass

MyyApp().run()

这里 myy.kv :

MainWidget:
<MainWidget>:
    cark:cark
    Cark:
        id: cark
        size: root.size
        pos: 0, 0
        canvas:
            Rotate:
                angle: self.angle
                origin: self.center

            Color:
                rgb: 1, 0, 0
            Ellipse:    
                size: min(self.size), min(self.size)
                pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)
        
## HERE IS 3 DIFFERENT WITDGETS LOCATED ON TOP OF EACH OTHER;        
    
    
        DayaI:
        DayaII:
        DayaIII:

标签: pythonkivyrotation

解决方案


推荐阅读