【发布时间】: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"
【问题讨论】:
-
使用 matplotlib 效果更好stackoverflow.com/questions/50012762/…