'Python and KivyMD: using a .json file / dict to dynamically add buttons and bind to a later defined function
'''
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.app import App
dict = {
"Button Name": "Create Event",
"Button Function": 'self.create_event'},
{
"Button Name": "Find Event",
"Button Function": "self.find_event"},
{
"Button Name": "Create Player",
"Button Function": "self.create_player"},
{
"Button Name": "Find Player",
"Button Function": "self.find_player"
}
class DemoBox(BoxLayout):
def __init__(self, **kwargs):
super(DemoBox, self).__init__(**kwargs)
self.orientation = "vertical"
for button in dict:
btn = Button(text=button['Button Name'])
btn.bind(on_press=button['Button Function'])
self.add_widget(button)
def create_event(self):
print("Going to the Create Event Page")
def create_player(self):
print("Sending to create player page")
def find_event(self):
print("Heading to find event page")
def find_player(self):
print('We are in the Find Player page')
class DemoApp(App):
def build(self):
return DemoBox()
if __name__ == "__main__":
DemoApp().run()
I am getting an error stating that self.create_event() is not callable
the actual code will be saved in a .json file and contain far more buttons, and will hopefully save code editing. Just allowing future buttons to be added by adding to the .json file / dict. Then defining the function, which will change page / execute code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
