正如標題所說,我試圖在每次更改值時打印出Switch小部件的值。我已經設法寫回調本身,但我似乎無法弄清楚在我的.kv文件中傳遞迴調的參數。回調使用.kv文件打印kivy Switch Widget的狀態
現在我得到的錯誤:回調()到底需要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'
感謝
我故意保持活動狀態:root.callback()爲空,因爲我不知道該放什麼。但是,謝謝你,這完美地解決了我的問題。我不明白,你可以添加'on_'屬性來改變時調用。 –