如何绑定两个类之间的值

时间:2015-03-23 21:02:07

标签: python kivy

class Controls(BoxLayout):   
    delta_value=NumericProperty()
    max_value=NumericProperty()
    solar_value=NumericProperty()
    hysteresis=NumericProperty()
    busy_image=StringProperty('125.png')
    temp_solar=NumericProperty()
    temp_pool=NumericProperty()


    def __init__(self, **kwargs):
        super(Controls, self).__init__(**kwargs)
        Clock.schedule_interval(self.set_value, 1)

    def PID_an_aus(self, instance, value):
        if value is True:
           pass

        else:
            pass

    def set_value(self, *args):


    # I want to pass temp_solar and temp_pool from PoolApp from read_temperature function



        print "Temperature",self.temp_solar, self.temp_pool 
        print "Delta",self.delta_value
        print "Max", self.max_value
        print self.hysteresis



class PoolApp(App):

    temperature=ListProperty()
    temp_sensor = DS18B20()

    temp_solar=NumericProperty(0.0)
    temp_pool=NumericProperty(0.0)




    def build(self):
        self.temp_sensor.start()

        Clock.schedule_interval(self.read_temperature, 0)


    def read_temperature(self, temperature):

        if not self.temp_sensor.temp_queue.empty():
            self.temperature = self.temp_sensor.temp_queue.get()
            self.temp_solar=self.temperature[0]
            self.temp_pool=self.temperature[1]
      #how to pass these values to Controls class       


    def on_stop(self):
        self.temp_sensor.running = False
        sys.exit()
if __name__ == '__main__':
    PoolApp().run()

1 个答案:

答案 0 :(得分:0)

我是新来的,我计划在评论中发布这个,但我的声誉低于50。所以,这是我的结论。 你的问题是你试图在课堂之间传递价值,并且我的理解是不可能的(如果我错了,有人会纠正我)。

您需要从这些类创建对象,然后在它们之间交换数据。我根据你的代码编写了虚拟代码(我没有使用它的传感器,所以我在列表中使用了两个虚拟值)

另外,我相信PoolApp中不需要“App”继承,因为你只需要常规类。所以,我创建了新的类(Tempar)以实际获取温度,然后在新的PoolApp中从该类创建对象。看一看,看看它会对你有所帮助。

顺便说一下,我的代码在python 3.x中,所以你必须将print命令更改为python2.x才能工作......

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, NumericProperty, ListProperty
from kivy.graphics.context import Clock
from kivy.uix.widget import Widget

class Controls(BoxLayout):   
    def __init__(self, **kwargs):
        self.temp_solar=NumericProperty()
        self.temp_pool=NumericProperty()
        self.delta_value=NumericProperty()
        self.max_value=NumericProperty()
        self.hysteresis=NumericProperty()
        #super(Controls, self).__init__(**kwargs)
        #Clock.schedule_interval(self.set_value, 1)

    def PID_an_aus(self, instance, value):
        if value is True:
           pass
        else:
            pass

    def set_value(self, *args):
        print("Temperature",self.temp_solar, self.temp_pool )
        print("Delta",self.delta_value)
        print("Max", self.max_value)
        print(self.hysteresis)

class Tempar():
    def __init__(self):
        self.temperature=[111,222]
        self.temp_sensor = []
        self.temp_solar=NumericProperty(0.0)
        self.temp_pool=NumericProperty(0.0)
        #self.temp_sensor.start()
        #Clock.schedule_interval(self.read_temperature, 0)

    def read_temperature(self, temperature):
        self.temp_solar=self.temperature[0]
        self.temp_pool=self.temperature[1]
        self.tmp=[self.temp_solar,self.temp_pool]
        return self.tmp


class PoolApp(App):
    #create object for each class and exchange values
    t=Tempar()
    c=Controls()
    c.temp_solar=t.read_temperature(t.temperature)[0]
    c.temp_pool=t.read_temperature(t.temperature)[1]
    print(c.temp_solar)
    print(c.temp_pool)
    #and here are your printouts
    c.set_value()        

if __name__ == '__main__':
    PoolApp().run()