簡體   English   中英

將功能綁定到kivy中的多個動態創建的按鈕?

[英]Bind a function to multiple dynamically created buttons in kivy?

問題

我想創建多個按鈕並將它們綁定到一個函數。 問題是,每當我單擊一個按鈕時,都會多次調用該函數。 事件連接似乎有問題。 當我查看按下按鈕時調用該函數的實例時,似乎立即從每個按鈕調用了該函數?

驗證碼:

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

如何創建多個按鈕,當按下這些按鈕時將調用單個功能?

編程指南»輸入管理»觸摸事件

默認情況下,觸摸事件將分派給所有當前顯示的窗口小部件。 這意味着小部件將接收觸摸事件,無論它是否在其物理區域內發生。

為了提供最大的靈活性,Kivy將事件分派給所有小部件,並讓它們決定如何對它們作出反應。 如果您只想響應小部件內的觸摸事件,則只需進行檢查。

使用self.collide_pointclick_on_button方法方法。 當它碰撞時,您應該只獲得一個按鈕。 請參閱我的示例以獲取詳細信息。

片段

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM