kivy:使用 on_press 事件更改根小部件中嵌套按钮的颜色
kivy: change the color of a nested button in the root widget with on_press event
我有一个小部件 class SidePanel
。它将包含 SidePanelButton
类型的元素。如果一个 SidePanelButton
被按下,它将发送 on_press
事件到作为 SidePanel
对象的根部件(每个按钮将发送相同的事件)。
在被调用的方法中,我想改变按下按钮的颜色(透视动画)。 self.background_color = <new value>
将不起作用,因为 self
是 SidePanel
对象。有没有办法在该函数中使用按下的按钮对象?或者任何其他方法?
完整代码和kv定义:
class SidePanelButton(Button):
title = StringProperty('')
'''Titel of the SidePannelButton
:attr:`title` is a :class:`~kivy.properties.StringProperty` and
defaults to ''.
'''
level = NumericProperty(0.1)
'''Indentation level of the SidePanelButton :attr:`title`.
:attr:`level` is a :class:`~kivy.properties.NumericProperty` and defaults to 0.1. The default range
is between 0.0 and 1.0.
'''
class SidePanel(StackLayout):
def __init__(self, **kwargs):
self.register_event_type('on_button1')
self.register_event_type('on_press')
super(SidePanel, self).__init__(**kwargs)
def on_press(self, *args):
# not working
#self.background_color = [1,1,1,1]
Logger.debug('SidePanel: on_press')
pass
def on_button1(self):
Logger.debug('SidePanel: on_button1')
pass
------------------------------------------------------------------
<SidePanelButton>:
size_hint: (1, None)
height: '48dp'
background_color: 0.3,0.3,0.3,1
background_normal: ''
background_down: ''
GridLayout:
rows: 1
height: self.parent.height
pos: self.parent.pos
Widget:
size_hint: (root.level,1)
Label:
markup: True
size_hint: (1,1)
text_size: self.size
text: root.title
font_size: self.height * 0.4
halign: 'left'
valign: 'middle'
<SidePanelButtonL1@SidePanelButton>:
level: 0.1
<SidePanelButtonL2@SidePanelButton>:
level: 0.25
<SidePanel>:
orientation: 'tb-rl'
canvas:
Color:
rgba: 0.3,0.3,0.3,1
Rectangle:
pos: self.pos
size: self.size
SidePanelButtonL1:
title: 'Button1'
on_press: root.dispatch('on_press')
on_release: root.dispatch('on_button1')
SidePanelButtonL2:
title: 'Button2'
您只需将按钮对象作为参数调用 on_button1
...
SidePanelButtonL1:
title: 'Button1'
on_press: root.dispatch('on_press')
on_release: root.on_button1(self)
...
def on_button1(self, bt1):
Logger.debug('SidePanel: on_button1 %s' % bt1)
pass
我有一个小部件 class SidePanel
。它将包含 SidePanelButton
类型的元素。如果一个 SidePanelButton
被按下,它将发送 on_press
事件到作为 SidePanel
对象的根部件(每个按钮将发送相同的事件)。
在被调用的方法中,我想改变按下按钮的颜色(透视动画)。 self.background_color = <new value>
将不起作用,因为 self
是 SidePanel
对象。有没有办法在该函数中使用按下的按钮对象?或者任何其他方法?
完整代码和kv定义:
class SidePanelButton(Button):
title = StringProperty('')
'''Titel of the SidePannelButton
:attr:`title` is a :class:`~kivy.properties.StringProperty` and
defaults to ''.
'''
level = NumericProperty(0.1)
'''Indentation level of the SidePanelButton :attr:`title`.
:attr:`level` is a :class:`~kivy.properties.NumericProperty` and defaults to 0.1. The default range
is between 0.0 and 1.0.
'''
class SidePanel(StackLayout):
def __init__(self, **kwargs):
self.register_event_type('on_button1')
self.register_event_type('on_press')
super(SidePanel, self).__init__(**kwargs)
def on_press(self, *args):
# not working
#self.background_color = [1,1,1,1]
Logger.debug('SidePanel: on_press')
pass
def on_button1(self):
Logger.debug('SidePanel: on_button1')
pass
------------------------------------------------------------------
<SidePanelButton>:
size_hint: (1, None)
height: '48dp'
background_color: 0.3,0.3,0.3,1
background_normal: ''
background_down: ''
GridLayout:
rows: 1
height: self.parent.height
pos: self.parent.pos
Widget:
size_hint: (root.level,1)
Label:
markup: True
size_hint: (1,1)
text_size: self.size
text: root.title
font_size: self.height * 0.4
halign: 'left'
valign: 'middle'
<SidePanelButtonL1@SidePanelButton>:
level: 0.1
<SidePanelButtonL2@SidePanelButton>:
level: 0.25
<SidePanel>:
orientation: 'tb-rl'
canvas:
Color:
rgba: 0.3,0.3,0.3,1
Rectangle:
pos: self.pos
size: self.size
SidePanelButtonL1:
title: 'Button1'
on_press: root.dispatch('on_press')
on_release: root.dispatch('on_button1')
SidePanelButtonL2:
title: 'Button2'
您只需将按钮对象作为参数调用 on_button1
...
SidePanelButtonL1:
title: 'Button1'
on_press: root.dispatch('on_press')
on_release: root.on_button1(self)
...
def on_button1(self, bt1):
Logger.debug('SidePanel: on_button1 %s' % bt1)
pass