[英]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()
class MyButton()
and implement on_touch_down()
method to check for collision of the touch with our widget. class MyButton()
并实现on_touch_down()
方法来检查触摸是否与我们的小部件冲突。 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)
<MyButton@Button>:
to classs rule, <MyButton>:
<MyButton@Button>:
重命名为类规则<MyButton>:
on_touch_down:
from MyButton:
MyButton:
删除所有on_touch_down:
MyButton:
<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"
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
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_press
或on_release
事件(请参阅按钮行为 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.