【问题标题】:Points of line of canvas in kivy are not updatedkivy中画布线的点未更新
【发布时间】:2019-04-28 09:04:22
【问题描述】:

我想制作一个实时图表,但我无法在我的代码中更新画布线的点,我可以更新标签和按钮的背景颜色和值,但使用画布我无法做到

我尝试过以类似的方式更新按钮和标签,但它根本没有更新,我输入了基本代码以及我如何更新标签和按钮,它运行并且你可以看到这条线,它应该有时钟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"

【问题讨论】:

标签: python canvas kivy line


【解决方案1】:

您可以使用包含点的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)

关键是kv 中对root.line_points 属性的引用为您设置了一个绑定,因此line_points 中的任何更改都会自动更新该行。

【讨论】:

  • 谢谢约翰,它现在更新了,但它在所有窗口中从一个角落开始而不是在相应区域中绘制,并且在我最大化时不尊重“self.x 和 self.y”进行调整窗口,我将继续尝试修复它。
  • 是的,可能只回答如何更新Line 点,而不是您使用什么值作为点。如果您希望该行响应更改的窗口大小,请查看bind
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多