简体   繁体   English

Python/Kivy 传递变量

[英]Python/Kivy passing variables

Struggling to pass a variable to kivy window.努力将变量传递给 kivy 窗口。 I have read similar threads all over the place but none of the fixes seem to work for me.我到处都读过类似的帖子,但似乎没有一个修复对我有用。 Im sure this is simple to someone who knows their way around tiny, unfortunately I don't.我敢肯定这对于知道自己的方式的人来说很简单,不幸的是我不知道。

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

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

Thanks in advance to anyone that can help me.在此先感谢任何可以帮助我的人。

Make update an instance method and use a StringProperty to update state property in your kv: update实例方法并使用StringProperty更新 kv 中的state属性:

main.py:主要.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:动作.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                                # <<<<<<<<<<<<

Here is a more generic simplified answer from the kivy documentation , look for the section called "Keyword arguments and init ()" because there are some other ways to do it as well.这是来自kivy 文档的更通用的简化答案,请查找名为“关键字参数和init ()”的部分,因为还有其他一些方法可以做到这一点。

The following code passes myvar to the build() method of MyApp.以下代码将 myvar 传递给 MyApp 的 build() 方法。 It does this by over-riding the init () of the Kivy App class by a new init () that calls App.它通过压倒一切的Kivy App类的的init()的一个新的init()调用应用程序执行此操作。 init () and then continues with whatever extra initialisation you want. init () 然后继续你想要的任何额外的初始化。 You can then store variables in the MyApp class instances and use them in build().然后,您可以将变量存储在 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