【问题标题】:How to bind the values between two classes如何绑定两个类之间的值
【发布时间】:2015-03-23 21:02:07
【问题描述】:
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()

【问题讨论】:

  • 我知道你们中的大多数人会忽略它并且不会回答。我尝试了不同的绑定方法,但最终值总是为零。我一定犯了同样的愚蠢错误,看不到,请帮忙

标签: python kivy


【解决方案1】:

我是新来的,我计划在 cmets 中发布此内容,但我的声望低于 50。所以,这是我的结论。 你的问题是你试图在类之间传递值,并让我理解这是不可能的(如果我错了,有人会纠正我)。

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

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

顺便说一句,我的代码在 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()

【讨论】:

  • 感谢您的回复。我已经尝试了代码并得到了一个错误:c.temp_solar=t.read_temperature(t,t.temperature)[0] TypeError: unbound method read_temperature() must be called with Tempar instance as first argument (got WidgetMetaclass instance)
  • 嗯,它对我有用。虽然,我认为我们应该在 Tempar 类中创建 init 并将温度和其他值放在该 init 中,然后我们应该调用 t=Tempar() (注意括号)...我现在不能检查它,因为我很着急,但我会尝试检查它......
  • 嘿,我对代码做了一点改动。你能再试试看它是否有效?
  • 在我加入括号之前它正在工作。我已经在我的程序中实现了代码,并且作为 Controls 的输出,打印返回正确的值并默认每秒一个。我试着找出原因?
  • 那是因为这条线:Clock.schedule_interval(self.set_value, 1)。在您的 init 中,您正在调用打印所有值的 set_value 函数 - 并且 Clock 函数设置为每秒执行该函数。如果你最初的问题解决了,你可以按左边的勾号把答案标记为好答案吗?
猜你喜欢
  • 2015-02-10
  • 2015-02-14
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多