如何绑定两个 类 之间的值

How to bind the values between two classes

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()

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

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

此外,我认为 PoolApp 中不需要像您那样的 "App" 继承,因为您只需要常规的 class。因此,我创建了新的 class (Tempar) 来实际获取温度,然后在新的 PoolApp 中从 class 创建了对象。看一看,看看它是否对您有帮助。

顺便说一句,我的代码在 python 3.x 中,因此您必须将打印命令更改为 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()