将功能绑定到kivy中的多个动态创建的按钮? [英] Bind a function to multiple dynamically created buttons in kivy?

查看:83
本文介绍了将功能绑定到kivy中的多个动态创建的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建多个按钮并将它们绑定到一个函数.问题是,每当我单击一个按钮时,都会多次调用该函数.事件连接似乎有问题.当我查看按下按钮时调用该函数的实例时,似乎立即从每个按钮中调用了该函数?!

I want to create multiple buttons and bind them to a function. The problem is that whenever I click on one button, the function is called multiple times. It seems to be a problem with the event connection. When I look at the instance that called the function when I pressed a button, it seems that the function gets called from every button at once?!

...
# 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)

示例输出:

当我单击一个按钮时,我得到的是什么!

Example Output:

What I get, when I click on a single 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的按钮时,我想要什么:

What I want when I press for example the button with ID 01:

<guiMain.ProjectSelectButton object at 0x0BA34260>
ID01 right mouse clicked

问题

如何创建多个按钮,这些按钮在按下时会调用一个功能?

Question

How do I create multiple buttons which will call a single function when they are pressed?

推荐答案

编程指南»输入管理»触摸事件

默认情况下,触摸事件将分派到所有当前显示的窗口小部件.这意味着小部件将接收触摸事件,无论它是否发生在其物理区域内.

By default, touch events are dispatched to all currently displayed widgets. This means widgets receive the touch event whether it occurs within their physical area or not.

为了提供最大的灵活性,Kivy派遣了 所有小部件的事件,并让他们决定如何对它们做出反应. 如果您只想响应小部件内的触摸事件,则可以 只需检查即可.

In order to provide the maximum flexibility, Kivy dispatches the events to all the widgets and lets them decide how to react to them. If you only want to respond to touch events inside the widget, you simply check.

解决方案

click_on_button 方法中使用self.collide_point方法.当它碰撞时,您应该只获得一个按钮.有关详细信息,请参阅我的示例.

Solution

Use self.collide_point method in click_on_button method. When it collides, you should get only one button. Please refer to my example for details.

class ProjectSelectButton(Button):
    def click_on_button(self, instance, touch, *args):
        print(instance)
        if self.collide_point(*touch.pos):
            if touch.button == 'right':
                print(self.id, "right mouse clicked")
            elif touch.buttom == 'left':
                print(self.id, "left mouse clicked")
            return True
        return super(ProjectSelectButton, self).on_touch_down(touch)

示例

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button


class CreateButton(Button):

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            if touch.button == "right":
                print(self.id, "right mouse clicked")
            elif touch.button == "left":
                print(self.id, "left mouse clicked")
            else:
                print(self.id)
            return True
        return super(CreateButton, self).on_touch_down(touch)


class OnTouchDownDemo(GridLayout):

    def __init__(self, **kwargs):
        super(OnTouchDownDemo, self).__init__(**kwargs)
        self.build_board()

    def build_board(self):
        # make 9 buttons in a grid
        for i in range(0, 9):
            button = CreateButton(id=str(i))
            self.add_widget(button)


class OnTouchDownApp(App):

    def build(self):
        return OnTouchDownDemo()


if __name__ == '__main__':
    OnTouchDownApp().run()

ontouchdown.kv

#:kivy 1.10.0

<CreateButton>:
    font_size: 50
    on_touch_down: self.on_touch_down

<OnTouchDownDemo>:
    rows: 3
    cols: 3
    row_force_default: True
    row_default_height: 150
    col_force_default: True
    col_default_width: 150
    padding: [10]
    spacing: [10]

这篇关于将功能绑定到kivy中的多个动态创建的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆