2

Does kivy support MouseEvent that is triggered on mouse_pos change without pressing mouse button?

I found in documentation this:

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.

However I cant implement it. I managed to bind it and add to on_motion function 'print('Hello world')' but it was triggered only by pressing-type events.

Thanks in advance

ikolim
  • 15,721
  • 2
  • 19
  • 29
Martin
  • 3,333
  • 2
  • 18
  • 39

2 Answers2

5

Solution

Bind mouse_pos to a callback. Please refer to example for details.

Window.bind(mouse_pos=self.mouse_pos)

Example

main.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()

Output

Img01

ikolim
  • 15,721
  • 2
  • 19
  • 29
1

You actually want to do:

Window.bind(mouse_pos=on_motion)
John Anderson
  • 35,991
  • 4
  • 13
  • 36