简体   繁体   中英

Kivy Label: on_touch_down event is triggered everywhere in the screen

I want to trigger my function when I click on the label but actually, it is triggered on the button too ! (and on all the screen to be more precise).

from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.label import Label

KV = """
BoxLayout:
    Button:
        text: "BUTTON"
"""

class MyFirstKivyApp(App):
    def build(self):
        self.box = Builder.load_string(KV)
        l = Label(text="LABEL")
        l.bind(on_touch_down=self.label_click)
        self.box.add_widget(l)
        return self.box

    def on_kv_post(self, base_widget):
        super().on_kv_post(base_widget)


    def label_click(self, w,event):
        print("Am I only triggered when touching the label ?")

MyFirstKivyApp().run()

Ok, not beautiful bur whatever... it works !

from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.label import Label

KV = """
BoxLayout:
    Button:
        text: "BUTTON"
"""

class MyFirstKivyApp(App):
    def build(self):
        self.box = Builder.load_string(KV)
        l = Label(text="LABEL")
        l.bind(on_touch_down=self.label_click)
        self.box.add_widget(l)
        return self.box

    def on_kv_post(self, base_widget):
        super().on_kv_post(base_widget)


    def label_click(self, w,touch):
        if w.collide_point(*touch.pos):
            print("Am I only triggered when touching the label ?")

MyFirstKivyApp().run()

Documentation can be found here

from kivy.lang.builder import Builder
from kivy.app import App
from kivy.uix.label import Label

KV = """
BoxLayout:
    Button:
        text: "BUTTON"
"""

class MyFirstKivyApp(App):
    def build(self):
        self.box = Builder.load_string(KV)
        l = Label(text="[ref=label]Label[/ref]", markup=True)
        l.bind(on_ref_press=self.label_click)
        self.box.add_widget(l)
        return self.box

    def on_kv_post(self, base_widget):
        super().on_kv_post(base_widget)


    def label_click(self, w,event):
        print("Am I only triggered when touching the label ?")

MyFirstKivyApp().run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM