如何在Kivy中实现鼠标悬停动画



我在Kivy中找到一种在按钮上实现鼠标悬停动画的方法时遇到了一些困难。互联网上有一些答案,尽管它们有点过时了。我使用的是python 3.7.7版本和Kivy 1.11.1。如果你知道一段可以帮助我简化流程的代码,请与我分享。Thx。

下面是一个如何检测鼠标悬停的示例。我不知道在kv:中有什么方法可以做到这一点

from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
kv = '''
FloatLayout:
Button:
id: butt
text: 'The Button'
size_hint: None, None
size: self.texture_size
pos_hint: {'center_x':0.5, 'center_y':0.5}
'''

class MouseOverApp(App):
def build(self):
Window.bind(mouse_pos=self.on_motion)
return Builder.load_string(kv)
def on_motion(self, src, mouse_pos):
if self.root.ids.butt.collide_point(*mouse_pos):
print('over Button at', mouse_pos, 'Do animation')

MouseOverApp().run()

最新更新