【问题标题】:Button actions on dynamically created kivy widgets动态创建的 kivy 小部件上的按钮操作
【发布时间】:2020-04-23 18:16:59
【问题描述】:

我在 add_row 函数中动态创建添加到此滚动视图列表中的行,它看起来/工作得非常好。问题是行有按钮,我希望这些按钮具有绑定到它们的事件/操作。我试过放 on_release,但它抛出了这个错误:

File "kivy\_event.pyx", line 419, in kivy._event.EventDispatcher.bind
 AssertionError: None is not callable

我不知道怎么做,所以我可以将按钮绑定到某个东西,同时仍然保持动态结构。请帮忙!这是我的代码:

def add_row(self, profile, user_id, user_name, percent):
    layout = GridLayout(rows=1, row_force_default=True, row_default_height=60)
    layout.add_widget(ImageButton(source=profile))
    layout.add_widget(Label(text="@" + user_name, color=(0, 0, 0, 1), font_size=20))
    layout.add_widget(Label(text=str(user_id), color=(0, 0, 0, 0), font_size=20))
    bsplit = GridLayout(rows=1)
    bsplit.add_widget(Button(background_normal='images/buttonbackgrounds/unfollow.png',
                             background_down='images/buttonbackgrounds/unfollow_select.png', size_hint_x=None, width=100, id=str(user_id), on_release=self.unfollow()))
    bsplit.add_widget(Button(background_normal='images/buttonbackgrounds/waitlist.png',
                             background_down='images/buttonbackgrounds/waitlist_select.png', size_hint_x=.5, border=(3,3,3,3), id=str(user_id)))
    layout.add_widget(bsplit)
    self.ids.widget_list.add_widget(layout)
    self.update_percent(percent)

def unfollow(self):
    print(self)

【问题讨论】:

    标签: python events kivy grid-layout


    【解决方案1】:

    您可以为此使用bind。

        button1 = Button(background_normal='images/buttonbackgrounds/unfollow.png',
                         background_down='images/buttonbackgrounds/unfollow_select.png',
                         size_hint_x=None, width=100, id=str(user_id))
        button1.bind(on_release=self.unfollow)
        bsplit.add_widget(button1)
    

    请注意,您只是将方法对象传递给bind,而不是使用“()”来执行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-22
      • 2021-11-03
      • 1970-01-01
      • 2022-06-26
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多