【发布时间】:2017-08-29 08:40:52
【问题描述】:
问题
我想创建多个按钮并将它们绑定到一个函数。问题是,每当我单击一个按钮时,都会多次调用该函数。似乎是事件连接的问题。当我查看按下按钮时调用该函数的实例时,似乎该函数被一次从每个按钮调用?!
KV代码:
...
# This is the button that I'am using
<ProjectSelectButton>:
height: 35
background_color: 0,0,1,1
on_touch_down: self.click_on_button(args[0], args[1])
...
# The buttons are added to this grid
ButtonsPlacedHere@GridLayout:
id: active_projects
cols: 1
size_hint_y: None
height: self.minimum_height
spacing: 1
...
Python 代码:
...
class ProjectSelectButton(Button):
def click_on_button(self, instance, touch, *args):
print(instance)
if touch.button == 'right':
print(self.id, "right mouse clicked")
else touch.buttom == 'left':
print(self.id, "left mouse clicked")
...
# The part of my programm that creates the buttons
# projects is a dictionary
for key, project in data_manager.resource_pool.projects.items():
print(project.project_name)
button= ProjectSelectButton(text=project.project_name, id=key, size_hint_y=None)
# Bind without KV-File (same result)
# label.bind(on_touch_up=self.click_on_button)
ids.active_projects.add_widget(button)
示例输出:
当我点击一个按钮时,我得到了什么!
<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA28F10>
ID02 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA28F10>
ID02 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA22C00>
ID03 right mouse clicked
<guiMain.ProjectSelectButton object at 0x0BA22C00>
ID03 right mouse clicked
当我按下 ID 为 01 的按钮时我想要什么:
<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked
问题
如何创建多个按钮,当它们被按下时会调用一个函数?
【问题讨论】:
-
你能发布你的整个应用还是太大了?
-
它很大,但我明天可以添加更多代码。
标签: python events kivy kivy-language