suroh
suroh

Reputation: 917

How can I print the position of the pointer every time the mouse is moved over a widget in kivy?

I want to print out the position of the pointer (mouse) everytime it passes over a widget. Actually to be more specific I'd like to print the current position of the mouse everytime it moves. Is that at all possible?

Right now all I've managed to do is:

  1. Print the position of the mouse whenever the mouse is clicked (in two different ways)

  2. Print the position of the widget when it's clicked

I'm sure it's easier than I think, thanks in advance.

from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.input.motionevent import MotionEvent

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

    def on_mouse_pos(self, pos):
        if Window.mouse_pos == self.ids.example_button:
            print("You've made it")
            # With out this if statement it does print the position every time 
            # That the mouse is moved. But can I print "You've made it" every time
            # that the mouse is moved over the position of a widget?
        print(pos)      

    def test_print(self):
        print(str(self.ids.example_button.pos))

    Window.bind(mouse_pos = on_mouse_pos)  


class MyScreenManager(Screen):
    pass


root_widget = Builder.load_string('''
MyScreenManager:
    MainScreen:


<MainScreen>:
    name: 'Example'

    Button:
        id: example_button
        text: 'Print position of widget when mouse touches it'
        size_hint: 1, .05
        pos_hint: {'x': 0, 'y': .5}
        on_release: root.test_print()

''')


class TestApp(App):
    def build(self):
        self.title = 'Example Application'
        return root_widget


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

Upvotes: 1

Views: 1863

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

A possible solution is to use mouse_pos:

from kivy.core.window import Window
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen 

class MainScreen(Screen):
    def on_mouse_pos(self, pos):
        print(pos)

    Window.bind(mouse_pos = on_mouse_pos)

class MyScreenManager(Screen):
    pass


root_widget = Builder.load_string('''
MyScreenManager:
    MainScreen:


<MainScreen>:
    name: 'Example'
    Button:
        id: example_button
        text: 'I am button'
        size_hint: 1, .05
        pos_hint: {'x': 0, 'y': .5}

''')


class TestApp(App):
    def build(self):
        self.title = 'Example Application'
        return root_widget


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

Update:

If you want to verify that the mouse point is inside a widget you must use collide_point.

from kivy.core.window import Window
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen 

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        Window.bind(mouse_pos = self.on_mouse_pos)

    def on_mouse_pos(self, instance, pos):
        if self.ids.example_button.collide_point(*pos):
            print("You've made it")

class MyScreenManager(Screen):
    pass


root_widget = Builder.load_string('''
MyScreenManager:
    MainScreen:


<MainScreen>:
    name: 'Example'
    Button:
        id: example_button
        text: 'I am button'
        size_hint: 1, .05
        pos_hint: {'x': 0, 'y': .5}

''')


class TestApp(App):
    def build(self):
        self.title = 'Example Application'
        return root_widget


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

Upvotes: 3

Related Questions