我正在使用python和kivy。 我的目标是使用TouchInput和kivy文件。 问题是我在kv文件中有一个请求:
<MainScreen>:
name: "main"
GridLayout:
rows: 1
Image:
source: 'image.png'
on_touch_down: if self.collide_point(*args[1].pos): root.doSomething()
单独运行效果很好,但是在我添加了TouchInput之后:
MainScreen:
def doSomething:
print("something")
def on_touch_down(self,touch):
print("down")
def on_touch_move(self, touch):
print("move")
def on_touch_up(self, touch):
pass
仅TouchInput被识别并且kivy文件不执行它的请求
答案 0 :(得分:0)
将以下内容添加到on_touch_down()
,on_touch_up()
和on_touch_move()
函数中。有关详细信息和输出,请参考示例。
if super(MainScreen, self).on_touch_...(touch):
return True
if not self.collide_point(*touch.pos):
return False
print('...')
return True
from kivy.app import App
from kivy.uix.screenmanager import Screen
class MainScreen(Screen):
def doSomething(self):
print("something")
def on_touch_down(self, touch):
if super(MainScreen, self).on_touch_down(touch):
return True
if not self.collide_point(*touch.pos):
return False
print('down')
return True
def on_touch_move(self, touch):
if super(MainScreen, self).on_touch_move(touch):
return True
if not self.collide_point(*touch.pos):
return False
print("move")
return True
def on_touch_up(self, touch):
if super(MainScreen, self).on_touch_up(touch):
return True
if not self.collide_point(*touch.pos):
return False
print("up")
return True
class TestApp(App):
def build(self):
return MainScreen()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.10.0
<MainScreen>:
name: "main"
GridLayout:
rows: 1
Image:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
source: 'kivyLogo.png'
on_touch_down:
if self.collide_point(*args[1].pos): root.doSomething()
Image:
canvas.before:
Color:
rgba: 0, 0, 1, 1
Rectangle:
pos: self.pos
size: self.size
source: 'raspberrypi.png'