繁体   English   中英

Python/Kivy 传递变量

[英]Python/Kivy passing variables

努力将变量传递给 kivy 窗口。 我到处都读过类似的帖子,但似乎没有一个修复对我有用。 我敢肯定这对于知道自己的方式的人来说很简单,不幸的是我不知道。

主文件

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

动作.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

在此先感谢任何可以帮助我的人。

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

主要.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()

动作.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                                # <<<<<<<<<<<<

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

以下代码将 myvar 传递给 MyApp 的 build() 方法。 它通过压倒一切的Kivy App类的的init()的一个新的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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM