Python / Kivy传递变量

时间:2018-01-25 19:54:29

标签: python python-3.x kivy kivy-language

努力将变量传递给kivy窗口。我已经阅读了相似的线程,但没有一个修复程序似乎适合我。我确信对于那些知道他们的方式很小的人来说这很简单,不幸的是我不会。

main.py

import kivy
from kivy.uix.togglebutton import ToggleButton
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.app import App
kivy.require('1.10.0')
from phue import Bridge
import nest
b = Bridge('xxx.xxx.x.xxx')
b.connect()
b.get_api()
lights = b.lights

class Controller(GridLayout):

    print("launching")


    def __init__(self):
            super(Controller, self).__init__()

    def KitchenSpot1(self,state):
        lights[0].name
        lights[0].on = state

    def update(dt):
        if b.get_light(1, 'on')== True:
            #print("down") # When this line is commented out I get an continuous accurate update on the status of the light, showing that its working.
            return 'down' # This is the part I want passed to the state criteria in the ivy window
        else:
            #print("up")# When this line is commented out I get an continuous accurate update on the status of the light, showing that its working.
            return 'down' # This is the part I want passed to the state criteria in the ivy window



class ActionApp(App):

    def build(self):

        Clock.schedule_interval(Controller.update, 1.0 / 60.0)
        return Controller()

myApp = ActionApp()
myApp.run()

action.kv

<Controller>:
    cols: 4
    rows: 3
    spacing: 10

    ToggleButton:
        id: KitchenSpot1Toggle
        text: "Kitchen Spot 1"
        on_press: root.KitchenSpot1(True) 

        #on_release: root.KitchenSpot1(False)
        #state1 = app.update.h
        state: Controller.update # This is the part that is throwing up the error.

错误:

      11:        #on_release: root.KitchenSpot1(False)
      12:        #state1 = app.update.h
 >>   13:        state: Controller.update
      14:
      15:
...
 NameError: name 'Controller' is not defined

提前感谢任何可以帮助我的人。

2 个答案:

答案 0 :(得分:1)

使update成为实例方法并使用StringProperty更新kv中的state属性:

<强> main.py:

import kivy
kivy.require('1.10.0')
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.togglebutton import ToggleButton
from phue import Bridge
import nest



b = Bridge('xxx.xxx.x.xxx')
b.connect()
b.get_api()
lights = b.lights


class Controller(GridLayout):
    state = StringProperty('normal')                        # <<<<<<<<<<<<

    def __init__(self, **kwargs):
        super(Controller, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1.0 / 60.0)

    def KitchenSpot1(self,state):
        lights[0].name
        lights[0].on = state

    def update(self, dt):
        if b.get_light(1, 'on'):
            self.state = 'down'                           # <<<<<<<<<<<<
        else:
            self.state = 'normal'                         # <<<<<<<<<<<<


class ActionApp(App):
    def build(self):
        return Controller()


if __name__ == "__main__":
    myApp = ActionApp()
    myApp.run()

<强> action.kv:

<Controller>:
    cols: 4
    rows: 3
    spacing: 10
    state: "normal"                                      # <<<<<<<<<<<<

    ToggleButton:
        id: KitchenSpot1Toggle
        text: "Kitchen Spot 1"
        on_press: root.KitchenSpot1(True)

        #on_release: root.KitchenSpot1(False)
        #state1 = app.update.h
        state: root.state                                # <<<<<<<<<<<<

答案 1 :(得分:0)

这是kivy documentation中更为通用的简化答案,请查找“关键字参数和 init ()”一节,因为还有其他一些方法可以做到这一点。

以下代码将myvar传递给MyApp的build()方法。通过使用调用App的新 init ()覆盖Kivy App类的 init ()来实现此目的。 init ()然后继续进行您想要的任何额外初始化。然后,您可以将变量存储在MyApp类实例中,并在build()中使用它们。

from kivy.app import App
from kivy.uix.label import Label

myvar = 'Hello Kivy'

class MyApp(App):

    def __init__(self, myvar, **kwargs):
        super(MyApp, self).__init__(**kwargs)
        self.myvar = myvar

    def build(self):
        widget = Label(text=self.myvar)
        return widget

if __name__ == '__main__':
    MyApp(myvar).run()