gpt4 book ai didi

python - 基维 - MotionEvent : on_mouse_pos

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:02 24 4
gpt4 key购买 nike

kivy是否支持在不按下鼠标按钮的情况下在mouse_pos发生变化时触发的MouseEvent?

我在文档中发现了这个:

def on_motion(self, etype, motionevent):
# will receive all motion events.
pass

Window.bind(on_motion=on_motion)

You can also listen to changes of the mouse position by watching mouse_pos.

但是我无法实现它。我设法绑定(bind)它并添加到 on_motion 函数 'print('Hello world')' 但它仅由按下类型事件触发。

提前致谢

最佳答案

解决方案

mouse_pos 绑定(bind)到回调。详情请引用示例。

Window.bind(mouse_pos=self.mouse_pos)

例子

主.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window


class MousePosDemo(BoxLayout):

def __init__(self, **kwargs):
super(MousePosDemo, self).__init__(**kwargs)
self.label = Label()
self.add_widget(self.label)
Window.bind(mouse_pos=self.mouse_pos)

def mouse_pos(self, window, pos):
self.label.text = str(pos)


class TestApp(App):
title = "Kivy Mouse Pos Demo"

def build(self):
return MousePosDemo()


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

输出

Img01

关于python - 基维 - MotionEvent : on_mouse_pos,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51342055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com