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

时间:2018-03-30 02:46:42

标签: python kivy

我正在使用Raspberry Pi为船上的方向舵反馈指示器创建程序。

几乎一个电位器连接到方向舵,可变电压被发送到连接到我的Pi的模数转换器。

我可以使用输入来显示我当前的舵角(以文本形式) 但是我希望它能够旋转我必须充当表盘(或表格)的图像

我希望角度不断更新并显示在GUI上。 如何获取一个函数来引用我的.KV文件中的旋转角度?

这将在程序首次运行时有效,但不会在整个程序中更新

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档案----------

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   

1 个答案:

答案 0 :(得分:0)

对于这些情况,您必须创建一个属性并使用角度执行绑定,对于这种情况,使用NumericProperty

<强>的.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

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