Search code examples
pythonpython-3.xnestedkivykivy-language

Function creates buttons but they can't trigger other functions


I'm Trying to build a dynamic layout in Kivy, my function generates buttons but they are not able to trigger any other function that would be responsible for the creation of labels:

def candidate_builder(self):
    file = open('GSUCandidates.txt', 'r')
    for names in file:
        names = names.rstrip()
        if 'President' in names:
            cbl_layout = self.ids['cs_grid']
            cn_label = Label(bold=True, text=names)
            cn_button = Button(id='pr', bold=True, text='Vote')
            cn_button.on_release = show()
            cbl_layout.add_widget(cn_label)
            cbl_layout.add_widget(cn_button)
            cbl_layout.height = cbl_layout.height + 250

            def show():
                vp_label = Label(bold=True, text=names)
                cpl_layout = self.ids['csp_grid']
                cpl_layout.add_widget(vp_label)

Solution

  • You can use:

    cn_button = Button(id='pr', bold=True, text='Vote', on_release=show)
    ...
    def show(*args):
    

    Or in case you want to send some variables to your method to should use lambda:

    cn_button = Button(id='pr', bold=True, text='Vote', on_release=lambda event: show()) # <- here you should have parentheses, where you can put anything you want
    ...
    def show():