基维:如何防止标签文本被旋转?

2024-06-14 23:14:09 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是旋转圆圈的代码,并将触碰和触碰移动计数显示为标签。代码将运行,但是,当我旋转圆圈(画布)时,标签正在旋转。我试图使标签居中,并防止标签旋转。我如何才能做到这一点?我如何使用碰撞方法来实现这一点?谢谢

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.properties import NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.core.window import Window
Window.size = (781.274, 599)
import math
kv = '''
<Dial>
    canvas:
        Rotate:
            angle: root.angle
            origin: self.center    
        Color:
            rgb: 255,0,0                      
        Line:
            circle:self.center_x, self.center_y, 112, 19, self.angle % -180
            width: 11      
        Color:
            rgb: 0,0,255    
        Line:
            circle:self.center_x, self.center_y, 112, 19, self.angle % 180
            width: 11
        Color:
            rgb: 255,255,255  
        Line:
            circle:self.center_x, self.center_y, 200, 500, self.angle % 45
            width: 10
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)/ 8.5,)           
        Color:
            rgba: .502,.502,.502,1 
        Ellipse: 
            pos: (850,410)
            size: (214,214)                 
'''
Builder.load_string(kv)
class Dial(Widget):
    def __init__(self, **kwargs):
        super(Dial, self).__init__(**kwargs)
        self.touch_move_count = 0
        self.touch_down_count = 0
        self.touch_move_Label = Label()
        self.touch_down_Label = Label()
        self.update_count()
        self.add_widget(self.touch_down_Label)
        self.add_widget(self.touch_move_Label)
    angle = NumericProperty(0)
    def update_count(self):
        self.touch_down_Label.text = str(self.touch_down_count)
        self.touch_move_Label.text = str(self.touch_move_count)
    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.prev_angle = calc if calc > 0 else 360 + calc
        self.tmp = self.angle
        self.touch_down_count += 1
        print(self.touch_down_count)
        self.update_count()
    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))
        new_angle = calc if calc > 0 else 360 + calc
        self.angle = self.tmp + (new_angle - self.prev_angle) % 360
        self.touch_move_count += 1
        print(self.touch_move_count)
        self.update_count()
class DialApp(App):
    def build(self):
        return Dial()
if __name__ == "__main__":
    DialApp().run()

Tags: fromimportselfmovecountcalcmathwidth
1条回答
网友
1楼 · 发布于 2024-06-14 23:14:09

Rotate:之前放一个PushMatrix:,在小部件画布的末尾放一个PopMatrix:。您可以查看文档,了解这些说明的作用以及为什么它们是合适的解决方案

相关问题 更多 >