我试图在Widget类中使用ScrollView但是当我放入on_touch_down方法时它不起作用。有什么问题?怎么解决这个?
这是一段代码:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget
Window.size = (520, 960)
class GameManager(Widget):
def __init__(self, **kwargs):
super(GameManager, self).__init__(**kwargs)
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
layout.bind(minimum_height=layout.setter('height'))
for i in range(100):
btn = Button(text=str(i), size_hint_y=None, height=40)
layout.add_widget(btn)
root = ScrollView(size_hint=(1, None), size=(Window.width/2, Window.height*3/4))
root.add_widget(layout)
self.add_widget(root)
#When I delete this method everything is fine
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
pass
class ScrollViewApp(App):
game = GameManager()
答案 0 :(得分:1)
touch_down
事件沿窗口小部件树传播,首先传递给根窗口小部件,然后让每个窗口小部件将触摸传递给其子窗口。问题是,你覆盖了on_touch_down
方法并且事件没有传播,连续你的ScrollView
没有响应这个事件。
您应该只调用父项的on_touch_downd
方法以允许事件传播:
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
pass
super(GameManager, self).on_touch_down(touch)