简体   繁体   中英

Kivy: How to prevent the label text from being rotated?

Below is the code for rotating the circles and showing the touch down and touch move count as labels. The code will run however, the labels are rotating as I rotate the circles (Canvas). I'm trying to center the labels and prevent the labels from getting rotated how can I achieve this? How do I use collide method to achieve this? Thanks

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()

Put a PushMatrix: before the Rotate: , and a PopMatrix: at the end of that widget's canvas. You can check the docs to see what these instructions do and why they're an appropriate solution here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM