切换按钮以更改其类之外的东西的属性 - Kivy



所以我很难理解如何将不同屏幕上的按钮的颜色更改为更改该按钮颜色的按钮(抱歉,如果这很难理解(。我对python相当陌生,对Kivy也很陌生,所以我会尽力解释这一点。

<ChannelOneWindow>中,我有enableSwitchOne ToggleButton通过if语句更改自身和colorMarkerOne的颜色。我还需要启用 SwitchOne 来更改<HomeWindow>中颜色标记 1 的颜色。我知道这可能是通过绑定完成的,但是 Kivy 框架没有很好的文档记录,我对此感到非常困难。谁能帮忙?

.kv 文件 -

#: import sm kivy.uix.screenmanager
WindowManager:
    HomeWindow:
    ChannelOneWindow:

<HomeWindow>:
    name: "home"
    FloatLayout:

        Button:
            pos_hint:{"x":0.0,"y":0.70}
            size_hint: 1.0, 0.2
            font_size: (root.width**2 + root.height**2) / 12**4
            background_normal: '0'
            id: armSwitch
            text: "SAFE"
            background_color: 0, 1, 0, 1
            on_press:
                root.updateText()
                root.updateColour()
                print("Sending To FPGA")


        Button:
            pos_hint:{"x":0,"y":0}
            size_hint: 0.16, 0.17
            font_size: (root.width**2 + root.height**2) / 14**4 #14**4
            text: "1"   
            on_release:
                root.manager.transition.direction = "left"
                root.channeloneBtn()


        ToggleButton:
            pos_hint:{"x":0,"y":0.17}
            size_hint: 0.16, 0.05
            id: colourMarker1
            font_size: (root.width**2 + root.height**2) / 14**4
            background_color: 5, 0, 0, 1            

<ChannelOneWindow>:
    name: "channelone"
    FloatLayout:
        Button:
            pos_hint:{"x":0.0,"y":0.90}
            size_hint: 0.20, 0.09
            font_size: (root.width**2 + root.height**2) / 15**4
            text: "Home"
            on_release:
                root.manager.transition.direction = "right" 
                root.homeBtn()  
        TextInput:
            font_size: (root.width**2 + root.height**2) / 15**4
            multiline: False
            pos_hint: {"x":0.20, "y":0.91}
            size_hint: 0.2, 0.08                    

        ToggleButton:
            pos_hint:{"x":0.35,"y":0.30}
            size_hint: 0.30, 0.10
            font_size: (root.width**2 + root.height**2) / 14**4
            background_normal: '0'
            id: enableSwitchOne
            text: "DISABLED"
            background_color: 1, 0, 0, 1
            on_state: 
                root.Enabled(*args)

        Button:
            pos_hint:{"x":0,"y":0}
            size_hint: 0.16, 0.17
            font_size: (root.width**2 + root.height**2) / 14**4 #14**4
            text: "1"   


        ToggleButton:
            pos_hint:{"x":0,"y":0.17}
            size_hint: 0.16, 0.05
            background_normal: '0'
            id: colourMarkerOne
            font_size: (root.width**2 + root.height**2) / 14**4
            background_color: 1, 0, 0, 1
            on_state: 
                root.Enabled(*args)

'''''''

.py文件 -

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import NumericProperty
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label

class HomeWindow (Screen):
    name = ObjectProperty(None)
    c = NumericProperty(0)

    def updateText(self):
        self.ids.armSwitch.text="ARMED"
    def updateColour(self):
        self.ids.armSwitch.background_color= 1, 0, 0, 1
    def channeloneBtn(self):
        self.reset()
        sm.current = "channelone"

    def reset(self):
          pass

class ChannelOneWindow (Screen):
    name = ObjectProperty(None)
    c = NumericProperty(0)


    def Enabled(self, *args):
        if args[1]=='down':
            self.Status = "Device on"
            self.ids.enableSwitchOne.background_color= 0, 1, 0, 1
            self.ids.colourMarkerOne.background_color= 0, 1, 0, 1
            self.ids.enableSwitchOne.text="ENABLED"
            print("Channel 1 Enabled")
            c = 1
        else:
            self.Status = "Device off"
            self.ids.enableSwitchOne.background_color= 1, 0, 0, 1
            self.ids.colourMarkerOne.background_color= 1, 0, 0, 1
            self.ids.enableSwitchOne.text="DISABLED"
            print("Channel 1 Disabled")
            c = 0

    def homeBtn(self):
        self.reset()
        sm.current = "home"

    def reset(self):
        pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("buzzcut.kv")
sm = WindowManager()

screens = [HomeWindow(name="home"),ChannelOneWindow(name="channelone") ]
for screen in screens:
    sm.add_widget(screen)

    sm.current = "home"
class BuzzcutApp(App):
    def build(self):
        return sm
if __name__ == "__main__":
    BuzzcutApp().run()

"">

如果需要更多信息,请告诉我,我会尽力而为。

使用 self.manager.get_screen("home")

self.manager.get_screen("home").ids.colourMarker1.background_color

基维屏幕管理器 » get_screen

get_screen(name)

返回与名称关联的屏幕小部件或引发 屏幕管理器异常(如果未找到(。

片段

class ChannelOneWindow(Screen):
    name = ObjectProperty(None)
    c = NumericProperty(0)
    def Enabled(self, *args):
        if args[1] == 'down':
            self.Status = "Device on"
            self.ids.enableSwitchOne.background_color = 0, 1, 0, 1
            self.ids.colourMarkerOne.background_color = 0, 1, 0, 1
            self.ids.enableSwitchOne.text = "ENABLED"
            self.manager.get_screen("home").ids.colourMarker1.background_color = 0, 1, 0, 1
            print("Channel 1 Enabled")
            c = 1
        else:
            self.Status = "Device off"
            self.ids.enableSwitchOne.background_color = 1, 0, 0, 1
            self.ids.colourMarkerOne.background_color = 1, 0, 0, 1
            self.ids.enableSwitchOne.text = "DISABLED"
            self.manager.get_screen("home").ids.colourMarker1.background_color = 1, 0, 0, 1
            print("Channel 1 Disabled")
            c = 0

最新更新