简体   繁体   English

自定义窗口小部件触发另一个自定义窗口小部件的on_touch_down事件

[英]custom widget triggers another custom widget's on_touch_down event

Whenever I press any area in MyWidget then it trigger the first MyButton 's on_touch_down event inside MyLayout class. 每当我按MyWidget任何区域时,它就会触发MyLayout类中的第一个MyButton的on_touch_down事件。 I am new to kivy and don't understand why this is happening. 我是新手,不知道为什么会这样。

<MyButton@Button>:
  background_normal: ''
  background_color: [1,0,1,1]
  font_size: "44dp"
  color:[1,1,0,1]
  border:(1,1,1,1)
  text_size:self.size

<MyWidget@Widget>:
  id:customwidget
  canvas.before:
    Color:
      rgba:0.2,0.44,0.66,1
    Rectangle:
      pos:self.pos
      size:self.size

<MyLayout>:
  MyWidget:
    id:mywidget

  MyButton:
    id:button1
    text: "User Name"
    spacing: "10dp"
    on_touch_down: button1.text= self.text + "?"
    opacity: .8

  MyButton:
    text: "ikinci"
    on_touch_down: root.export_to_png("filename.png")
  MyButton:
    text: "ucuncu"

And this is python: 这是python:

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget

class MyLayout(BoxLayout):
    pass

class KivyApp(App):
    def build(self):
        return MyLayout()

if __name__ == "__main__":
    KivyApp().run()

Python code Python代码

  1. Create a class MyButton() and implement on_touch_down() method to check for collision of the touch with our widget. 创建一个class MyButton()并实现on_touch_down()方法来检查触摸是否与我们的小部件冲突。

Snippet - Python code 代码段-Python代码

class MyButton(Button):

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            print("\tMyButton.on_touch_down:")
            if self.text == 'User Name':
                self.text= self.text + "?"
            elif self.text == 'ikinci':
                App.get_running_app().root.export_to_png("filename.png")
            return True
        return super(MyButton, self).on_touch_down(touch)

kv file KV文件

  1. Rename dynamic class <MyButton@Button>: to classs rule, <MyButton>: 将动态类<MyButton@Button>:重命名为类规则<MyButton>:
  2. Remove all on_touch_down: from MyButton: MyButton:删除所有on_touch_down: MyButton:

Snippet - kv file 片段-KV文件

<MyButton>:
    background_normal: ''
    background_color: [1,0,1,1]
    font_size: "44dp"
    color:[1,1,0,1]
    border:(1,1,1,1)
    text_size:self.size
...
    MyButton:
        id:button1
        text: "User Name"
        spacing: "10dp"
        opacity: .8

    MyButton:
        text: "ikinci"

    MyButton:
        text: "ucuncu"

Touch event basics 触摸事件基础

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. 这意味着小部件将接收触摸事件,无论它是否在其物理区域内发生。

In order to provide the maximum flexibility, Kivy dispatches the events to all the widgets and lets them decide how to react to them. 为了提供最大的灵活性,Kivy将事件分派给所有小部件,并让它们决定如何对它们作出反应。 If you only want to respond to touch events inside the widget, you simply check: 如果您只想响应小部件内的触摸事件,只需检查:

 def on_touch_down(self, touch): if self.collide_point(*touch.pos): # The touch has occurred inside the widgets area. Do stuff! pass 

Output 产量

Img01-单击的按钮用户名 Img02-单击的按钮-ikinci

The on_touch events are not specific to any widget, but are propagated to all your widgets. on_touch事件并非特定于任何窗口小部件,而是传播到所有窗口小部件。 See the documentation . 请参阅文档 If you only want your Button to respond, you probably want to use on_press or on_release events instead (See button behavior ). 如果只希望Button做出响应,则可能要使用on_presson_release事件(请参阅按钮行为 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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