Точки линии холста в кивах не обновляются - PullRequest
0 голосов
/ 28 апреля 2019

Я хочу сделать график в реальном времени, но я не могу обновить точки линии холста в своем коде, я могу обновить цвета фона и значения надписей и кнопок, но с холстом я не могу это сделать

Я пробовал аналогичным образом, когда я обновляю кнопки и метки, но он просто не обновляется, я ставлю основной код и как я обновляю метки и кнопки, он работает, и вы можете увидеть строку с часамичерез 5 секунд он должен обновиться как другой материал, спасибо за вашу помощь.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
from kivy.graphics import Color, Rectangle
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.properties import NumericProperty, ReferenceListProperty,\
    ObjectProperty
from kivy.vector import Vector

Prin2ON = 0
points = (0,0,0,0,0,0)

class Site21(Button):
    pass
class Site22(Button):
    pass
class Site23(Button):
    pass
class Hora(Button):
    pass
class Horas12(Button):
    pass
class Horas24(Button):
    pass
class Semana(Button):
    pass
class Mes(Button):
    pass

class WelcomeScreen(GridLayout):
    def __init__(self):
        super(WelcomeScreen, self).__init__()

        number = 1
        def count_it2(number):
            global Prin2ON

            #Originally I update these values from a db
            number1,number2,number3,number4 = 1,2,3,4
            self.ids.tempP2.text = str(number1)
            if number1 <= 25:

               #Here is how I update background colors and it works
               self.ids.tempP2.background_color = 0, 1, 0, 1
            if number1 > 26 and number1 <= 28:
               self.ids.tempP2.background_color = 1, 1, 0, 1
            if number1 > 28:
               self.ids.tempP2.background_color = 1, 0, 0, 1
            self.ids.humeP2.text = str(number2)
            if number2 <= 20:
               self.ids.humeP2.background_color = 0, 1, 0, 1
            if number2 > 21 and number2 <= 40:
               self.ids.humeP2.background_color = 1, 1, 0, 1
            if number2 > 41:
               self.ids.humeP2.background_color = 1, 0, 0, 1
            self.ids.humoP2.text = str(number3)
            if number3 <= 40:
               self.ids.humoP2.background_color = 0, 1, 0, 1
            if number3 > 41 and number3 <= 45:
               self.ids.humoP2.background_color = 1, 1, 0, 1
            if number3 > 46:
               self.ids.humoP2.background_color = 1, 0, 0, 1
            if Prin2ON == number4:
                self.ids.Princip2.color = 1, 0, 0, 1
                self.ids.tempP2.text = str("No data")
                self.ids.tempP2.background_color = 0, 0, 0, 0
                self.ids.humeP2.text = str("No data")
                self.ids.humeP2.background_color = 0, 0, 0, 0
                self.ids.humoP2.text = str("No data")
                self.ids.humoP2.background_color = 0, 0, 0, 0
            else:
                self.ids.Princip2.color = 1, 1, 1, 1
            Prin2ON = number4

            Clock.schedule_once(lambda dt: count_it2(number), 5)

        def count_it8(gra):
            global points

            #Here is where I don't know how update the points
            self.ids.grapic.line = (self.x, self.y+100, self.x+200, self.y+150, self.x+300, self.y+500)
            Clock.schedule_once(lambda dt: count_it8(gra), 5)

        Clock.schedule_once(lambda dt: count_it2(0), 0)
        Clock.schedule_once(lambda dt: count_it8(0), 0)

class MainApp(App):
    def build(self):
        self.title = 'Monitoring system'
        return WelcomeScreen()


if __name__ == "__main__":
    app = MainApp()
    app.run()

А вот код kv:

# -*- coding: cp1252 -*-
<WelcomeScreen>:
    display: display
    rows: 1
    GridLayout:
        cols: 4

        Label:
            id: grr
            text: "Estación"
        Label:
            id: display
            text: "Temperatura [C]"
        Label:
            id: display
            text: "Humedad [%]"
        Label:
            id: display
            text: "Humo [ppm]"


        Label:
            id:Princip2
            text: "Prin2"
        Site21:
            id:tempP2
            text: ""
            on_release: root.site_one()
        Site22:
            id:humeP2
            text: ""
            on_release: root.site_one()
        Site23:
            id:humoP2
            text: ""
            on_release: root.site_one()



    BoxLayout:
        orientation: "vertical"
        GridLayout:
            id: grapic
            cols: 1
            size: 25, 200
            canvas:
                Color:
                    rgba: .1, 1, .1, .9
                Line:
                    width: 1.2
                    points: (self.x, self.y+50, self.x+200, self.y+100, self.x+300, self.y+200)
                Line:
                    width: 1.
                    rectangle: (self.x+30, self.y+100, self.width-50, self.height-200)

        GridLayout:
            size_hint: 1, 0.15
            cols: 5
            Hora:
                text: "Hora"
                on_release: root.site_one()
            Horas12:
                text: "12 Horas"
                on_release: root.site_one()
            Horas24:
                text: "24 Horas"
                on_release: root.site_one()
            Semana:
                text: "Semana"
                on_release: root.site_one()
            Mes:
                id: display
                text: "Mes"
                on_release: root.site_one()
        #Label:
            #id: display
            #font_size: dp(50)
            #text: "0"

1 Ответ

0 голосов
/ 28 апреля 2019

Вы можете обновить свою линию, используя ObjectProperty, который содержит точки, а затем просто обновить это ObjectProperty. Для начала добавьте ObjectProperty к вашему классу WelcomeScreen как:

class WelcomeScreen(GridLayout):
    line_points = ObjectProperty((0,0,0,0))

Затем укажите это свойство в вашем kv:

BoxLayout:
    orientation: "vertical"
    GridLayout:
        id: grapic
        cols: 1
        size: 25, 200
        canvas:
            Color:
                rgba: .1, 1, .1, .9
            Line:
                width: 1.2
                points: root.line_points  # references ObjectProperty
            Line:
                width: 1.
                rectangle: (self.x+30, self.y+100, self.width-50, self.height-200)

Теперь вы можете просто обновить объект line_points, чтобы внести необходимые изменения:

    def count_it8(gra):
        global points

        #Here is where I don't know how update the points
        self.line_points = (self.x, self.y+100, self.x+200, self.y+150, self.x+300, self.y+500)
        Clock.schedule_once(lambda dt: count_it8(gra), 5)

Ключ в том, что ссылка на свойство root.line_points в kv устанавливает привязку для вас, так что любое изменение в line_points автоматически обновляет строку.

...