简体   繁体   English

Kivy:如何根据输入连续旋转图像

[英]Kivy: How to Rotate an Image continually, based on an input

I am using a Raspberry Pi to create a program for a rudder feedback indicator on a boat. 我正在使用Raspberry Pi为船上的方向舵反馈指示器创建程序。

Pretty much a potentiometer is connected to the rudder and a variable voltage is sent to an Analog to digital converter connected to my Pi. 电位器几乎连接到舵,可变电压发送到连接到我的Pi的模数转换器。

I can use the input to display my current rudder angle (in text form) however I want it to be able to rotate an image I have to act as the dial (or guage) 我可以使用输入来显示当前的舵角(以文本形式),但是我希望它能够旋转图像,而我必须充当表盘(或标尺)

I want the angle to be constantly updated and appear on the GUI. 我希望角度不断更新并出现在GUI上。 How do I get a function to refer back to the rotation angle in my .KV file? 我如何获得一个功能来返回我的.KV文件中的旋转角度?

This will work when the program is first run but will not update throughout the program 首次运行该程序时它将起作用,但不会在整个程序中进行更新

class RudderAngle(Screen):
def __init__(self, **kwargs):
    super(RudderAngle, self).__init__(**kwargs)
    Clock.schedule_interval(self.Print_Rudder,0.02)
def Print_Rudder(self, *args):
    angle = mcp.read_adc(7)
    deg = angle/17.0
    dega = angle/17.0

    if deg<=30:
        ri = (30-deg)
        colour = [0, 1, 0, 1]
        angle = 90-dega*3
    if deg==30:
        ri = (0)
        colour = [0,0,0,1]
        angle = 0
    if deg>30:
        ri = (deg-30)
        colour = [1, 0, 0, 1]
        angle =-(dega*3)+90

    ri = math.floor(ri)
    ri = int(ri)
    ri = str(ri)

    rai = self.ids.rudderposition
    ind1 = self.ids.ind
    rai.text = ri
    rai.color = colour
    ind1.angle = angle
    return angle

----------KV File---------- ---------- KV文件----------

FloatLayout:
        id: rudder_angle
        padding: 10

        canvas.before:
            Color:
                rgba: .95, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size

        Label:
            font_size: 100
            id: rudderposition
            text: '0'
            pos_hint: {"center_x": .5, "center_y": .6}
            background_color: (1, 1, 1, 1)


        Image:
            id: ind
            source: 'rai-pics/dial.png'
            size_hint: 1,1
            pos_hint: {"center_x": .5, "center_y": .5}
        Image:
            source: 'rai-pics/ri.png'
            size_hint: 0.7,0.5
            pos_hint: {"center_x": .5, "center_y": .5}
            canvas.before:
                PushMatrix
                Rotate:
                    #angle: root.Print_Rudder()
                    axis: 0, 0, 1
                    origin: root.center
            canvas.after:
                PopMatrix   

For these cases you must create a property and perform the binding with the angle, for this case NumericProperty is used: 对于这些情况,必须创建一个属性并使用角度进行绑定,在这种情况下,将使用NumericProperty

.py .py

...
from kivy.properties import NumericProperty


class RudderAngle(Screen):
    angle_of_rudder = NumericProperty()
    def __init__(self, **kwargs):
        super(RudderAngle, self).__init__(**kwargs)
        Clock.schedule_interval(self.Print_Rudder,0.02)

    def Print_Rudder(self, *args):
        ...
        ind1.angle = angle
        self.angle_of_rudder = angle

root = Builder.load_file("main.kv")

class TestApp(App):
    def build(self):
        return root

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

main.kv 主.kv

RudderAngle:
    name : 'screen'
    FloatLayout:
        id: rudder_angle
        padding: 10

        canvas.before:
            Color:
                rgba: 0.95, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size

        Label:
            font_size: 100
            id: rudderposition
            text: '0'
            pos_hint: {"center_x": .5, "center_y": .6}
            background_color: (1, 1, 1, 1)


        Image:
            id: ind
            source: 'rai-pics/dial.png'
            size_hint: 1,1
            pos_hint: {"center_x": .5, "center_y": .5}

        Image:
            source: 'rai-pics/ri.png'
            size_hint: 0.7,0.5
            pos_hint: {"center_x": .5, "center_y": .5}
            canvas.before:
                PushMatrix
                Rotate:
                    angle: root.angle_of_rudder
                    axis: 0, 0, 1
                    origin: root.center
            canvas.after:
                PopMatrix 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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