gpt4 book ai didi

python - 制作稍微更高效的函数结构

转载 作者:太空宇宙 更新时间:2023-11-03 14:11:43 25 4
gpt4 key购买 nike

我有一堆灯想要控制。我不想让每个按钮状态更改调用一个唯一的函数,而是想尝试拥有一个多用途函数,因为这就是函数的用途(据我所知)。

按钮调用函数:

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

功能:

def changeKS1(self,change):
if change==1 and b.get_light(1, 'on'):
self.KitchenSpot1(False)
else:
self.KitchenSpot1(True)

然后该函数调用此函数,使用第三部分库以物理方式更改灯光的状态。

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

我在函数内部传递“1”的原因是因为它不喜欢在其中传递任何内容(我不知道为什么它不喜欢它)。如果你还没有猜到的话,我是新手。我有一点 cpp 微 Controller 背景,但我正在尝试了解 python 和基于 PC 的编程。我正在寻找一些关于如何最好地浓缩它并使其尽可能高效的建议。我可能对 python 不太了解,但是,我知道我不应该输入几乎相同的内容 30 次。

预先感谢任何可以分享他们智慧的人。

注意到我正在使用 kivy 和 python 来生成按钮。

完整的main.py代码:

from kivy.properties import StringProperty
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('xx.xxx.xxx.xxx')
b.connect()
b.get_api()
lights = b.lights

class Controller(GridLayout):
state = StringProperty('down')

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 changeKS1(self,change):
if change==1 and b.get_light(1, 'on'):
self.KitchenSpot1(False)
else:
self.KitchenSpot1(True)


def KitchenSpot2(self,state):
lights[1].name
lights[1].on = state

def KitchenSpot3(self,state):
lights[2].name
lights[2].on = state

def OfficeSpot1(self,state):
lights[3].name
lights[3].on = state

def OfficeSpot2(self,state):
lights[4].name
lights[4].on = state

def OfficeSpot3(self,state):
lights[5].name
lights[5].on = state

def OfficeSpot4(self,state):
lights[6].name
lights[6].on = state

def JuliaBedside(self,state):
lights[7].name
lights[7].on = state

def JohnBedside(self,state):
lights[8].name
lights[8].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.changeKS1(1)

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


ToggleButton:
text: "Kitchen Spot 2"

Button:
text: "Kitchen Spot 3"

Button:
text: "Kitchen Spot 4"

Button:
text: "Office Spot 1"

Button:
text: "Office Spot 2"

Button:
text: "Office Spot 3"

Button:
text: "Office Spot 4"

更新:Python程序:

    def lightcontrol(self,lightnumber):
if b.get_light(1, 'on'):
lights[lightnumber].name
lights[lightnumber].on (False)
#self.KitchenSpot1(False)
else:
lights[lightnumber].name
lights[lightnumber].on (True)
#self.KitchenSpot1(True)

Kivy 按钮:

    ToggleButton:
id: KitchenSpot1Toggle
text: "Kitchen Spot 1"
on_press: root.lightcontrol(0)

最佳答案

让每个按钮调用相同函数,但使用不同的参数。

# Add the a number parameter here based on what you've
def KitchenSpot(self,state, light_index):
lights[light_index].name
lights[light_index].on = state

然后在KV文件中,

Button:
text: "Kitchen Spot 3"
on_press: root.KitchenSpot(state, light_index = 3)

Button:
text: "Kitchen Spot 4"
on_press: root.KitchenSpot(state, light_index = 4)

您只需创建函数一个,每个按钮都会传递相关的 light_index 编号。

关于python - 制作稍微更高效的函数结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48453695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com