因此,我在理解如何将位于不同屏幕上的按钮的颜色更改为更改按钮颜色的按钮时遇到麻烦(很抱歉,如果很难理解的话)。我是python的新手,也是Kivy的新手,所以我会尽力解释一下。
在background-color
中,我有一个enableSwitchOne ToggleButton通过if语句更改其自身和colourMarkerOne的颜色。我还需要enableSwitchOne来更改<ChannelOneWindow>
中colourMarker1的颜色。我知道这可能是通过绑定来完成的,但是Kivy框架的文档尚不完善,对此我感到非常困难。有人可以帮忙吗?
.kv文件-
<HomeWindow>
.py文件-
#: 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)
'''''''
''''''
让我知道是否需要更多信息,我会尽力而为。
答案 0 :(得分:0)
使用self.manager.get_screen("home")
self.manager.get_screen("home").ids.colourMarker1.background_color
get_screen(name)
返回与名称关联的屏幕小部件或引发一个 如果未找到ScreenManagerException。
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