正如标题所说,每次更改值时,我都会尝试打印出 Switch 小部件的值。我已经设法编写了回调本身,但我似乎无法弄清楚在我的 .kv 文件中传递回调的参数。
现在我收到错误消息:'callback() 正好需要 2 个参数(给定 1 个)'
from kivy.config import Config
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '300')
Config.set('graphics', 'resizable', 0)
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.switch import Switch
Builder.load_file('hueLayout.kv')
class hueLayout(BoxLayout):
pwr1_switch = ObjectProperty()
def callback(instance, value):
print 'instance: ', instance
print 'value: ', value
#pwr1_switch.bind(pwr1_switch, active=callback)
class HueController(App):
def build(self):
#self._app_window_size = 5, 20
return hueLayout()
if __name__ == '__main__':
Config.write()
HueController().run()
<hueLayout>:
#size_hint: .5, .5
#pos_hint: {'center_x': .5, 'center_y': .5}
#height: '200dp'
#width: '100dp'
pwr1_switch: pwr1_input
TabbedPanel:
do_default_tab: False
TabbedPanelItem:
text: 'Master'
GridLayout:
cols: 3
Switch:
id: pwr1_input
active: root.callback()
TabbedPanelItem:
text: 'Light 1'
BoxLayout:
TabbedPanelItem:
text: 'Light 2'
TabbedPanelItem:
text: 'Light 3'
谢谢