动态创建的 kivy 小部件上的按钮操作
Button actions on dynamically created kivy widgets
我在 add_row 函数中动态创建行,这些行被添加到这个滚动视图列表中,而且 looks/works 非常好。问题是行有按钮,我希望这些按钮绑定 events/actions。我尝试放置 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)
您可以使用 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
,而不是使用“()”来执行它。
我在 add_row 函数中动态创建行,这些行被添加到这个滚动视图列表中,而且 looks/works 非常好。问题是行有按钮,我希望这些按钮绑定 events/actions。我尝试放置 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)
您可以使用 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
,而不是使用“()”来执行它。