簡體   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