代码之家  ›  专栏  ›  技术社区  ›  Martin

Kivy-MotionEvent:在鼠标上

  •  1
  • Martin  · 技术社区  · 7 年前

    Kivy是否支持在不按鼠标键的情况下在鼠标位置更改时触发的mouseevent?

    我在文件中发现:

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

    你也可以通过观察鼠标位置来收听鼠标位置的变化。

    但是我不能执行它。我成功地将其绑定并添加到on_motion函数“print('hello world')”中,但它只是通过按type events触发的。

    提前谢谢

    2 回复  |  直到 7 年前
        1
  •  3
  •   ikolim    7 年前

    解决方案

    捆绑 mouse_pos 回拨电话。详情请参阅示例。

    Window.bind(mouse_pos=self.mouse_pos)
    

    例子

    Me.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

        2
  •  1
  •   John Anderson    7 年前

    你真的想:

    Window.bind(mouse_pos=on_motion)