我正在使用kivy,我已将我的应用程序拆分为.py
和.kv
个文件。在.kv
文件中,我创建了一个带有两个按钮和一个滑块的popup
窗口:
<SettingsPopup>:
cols:1
Label:
text: root.text
GridLayout:
cols: 2
size_hint_y: None
height: '44sp'
Label:
id: mylabel
text: 'XXX'
Slider:
value: 50
max: 10
on_value: root.dispatch('on_brightness', args[1])
Button:
text: 'Yes'
on_release: root.dispatch('on_settings','yes')
Button:
text: 'No'
on_release: root.dispatch('on_settings', 'no')
我将on_settings
事件绑定到python
方法,它们按预期工作,即打印字符串。
以下是SettingsPopup
类:
class SettingsPopup(GridLayout):
text = StringProperty()
a = NumericProperty(1.0)
def __init__(self, **kwargs):
self.register_event_type('on_settings')
self.register_event_type('on_brightness')
super(SettingsPopup, self).__init__(**kwargs)
def on_settings(self, *args):
pass
def on_brightness(self, *args):
pass
以及另一个类中用于测试功能的方法:
def settings(self, *largs):
content = SettingsPopup(text='Please edit the experimental settings')
content.bind(on_settings=self._on_settings)
content.bind(on_settings=self._on_brightness)
self.popup = Popup(title="Experimental Settings",
content=content,
size_hint=(None, None),
size=(200, 200),
auto_dismiss=False)
self.popup.open()
def _on_settings(self, instance, answer):
print "USER Name: ", repr(answer)
self.popup.dismiss()
def _on_brightness(self, instance, answer):
print "Brightness Level: ", repr(answer)
self.popup.dismiss()
描述按钮的代码基于SO的另一个例子,我没有参考。但是,我尝试将其扩展为滑块,但on-brightness
永远不会触发。我尝试添加a = NumericProperty(1.0)
,但它没有效果。
我开始在on_value: print(args[1])
文件中使用.kv
,当我移动滑块时打印出来,所以我知道该事件有效,但我无法在python中调用该事件。