Please forgive if my question isnt placed properly, i am new to this whole subject. In my python file i have a callback function for my switches, which i create in my .kv file.
from kivy.app import App
from kivy.uix.pagelayout import PageLayout
class PageLayout(PageLayout):
statusMsg = 'Empty Status.'
def switch_callback(self, switchObject, switchValue):
#Switch values are True and False
if(switchValue):
statusMsg = '{0} enabled'.format(switchObject)
print(statusMsg)
else:
statusMsg = '{0} disabled'.format(switchObject)
print(statusMsg)
return statusMsg
def __init__(self):
super(PageLayout, self).__init__()
class myGUI(App):
def build(self):
return PageLayout()
if __name__ == "__main__":
myGUI().run()
Kivy File (myGUI.kv):
<PageLayout>:
BoxLayout:
BoxLayout:
Label:
text: 'TestLabel'
Switch:
id: switchCal
active: False
on_active: root.switch_callback(*args)
now i can print the called object argument but not the id. Do i have to use another variable, or is id not passed with *args? My shell looks like this:
<kivy.uix.switch.Switch object at 0x1111B258> enabled
I tried switchObject.id but it doesnt work. Thanks in advance!
One way to find the switch's id
, is a for loop
with the root
's ids
.
for i_d in self.ids:
if switchObject == self.ids[i_d]:
print(i_d)
break
You have also to rename your PageLayout
class to something else, because its name conflicts with kivy's PageLayout
.
Full code for the py
part:
from kivy.app import App
from kivy.uix.pagelayout import PageLayout
class PageLayoutX(PageLayout):
statusMsg = 'Empty Status.'
def switch_callback(self, switchObject, switchValue):
for i_d in self.ids:
if switchObject == self.ids[i_d]:
print(i_d)
break
# Switch values are True and False
if (switchValue):
statusMsg = '{0} enabled'.format(switchObject)
print(statusMsg)
else:
statusMsg = '{0} disabled'.format(switchObject)
print(statusMsg)
return statusMsg
def __init__(self):
super(PageLayoutX, self).__init__()
class myGUI(App):
def build(self):
return PageLayoutX()
if __name__ == "__main__":
myGUI().run()
... and the kv
part
<PageLayoutX>:
BoxLayout:
BoxLayout:
Label:
text: 'TestLabel'
Switch:
id: switchCal
active: False
on_active: root.switch_callback(*args)