我想制作一个实时图形,但是我无法在代码中更新画布的线条点,可以更新背景颜色以及标签和按钮的值,但是对于画布我无法做到< / p>
我尝试过与更新按钮和标签类似的方式,但是它并没有更新,我输入了基本代码以及如何更新标签和按钮,它运行起来,您可以看到带有时钟的行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"
答案 0 :(得分:0)
您可以通过使用包含点的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
中的任何更改都将自动更新该行。