每次将鼠标移动到kivy上的小部件上时,如何打印指针的位置?

时间:2018-02-25 18:23:51

标签: python python-3.x kivy

我希望每次通过窗口小部件时打印出指针(鼠标)的位置。实际上更具体的是我想在每次移动时打印鼠标的当前位置。那可能吗?

现在我所能做的就是:

  1. 单击鼠标时打印鼠标的位置 (以两种不同的方式)

  2. 点击窗口小部件时的位置

  3. 我确信这比我想象的要容易,提前谢谢。

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

1 个答案:

答案 0 :(得分:1)

可能的解决方案是使用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()

更新

如果要验证鼠标点是否位于窗口小部件内,则必须使用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()