简体   繁体   中英

Kivy Python Mouse Position

I am trying to write a app where i can get the mouse position all the time. right now they code i have needs to be clicked in order to get mouse position BUT i want to get position all the time.

from kivy.app import App
from kivy.uix.label import Label

class MousePos(App):
    def build(self):
        from kivy.core.window import Window
        self.label = Label()
        Window.bind(mouse_pos=lambda w, p: setattr(self.label, 'Mouse Position', str(p)))
        return self.label

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

What can I add to get mouse position as I drag my mouse on the screen I get its position?

You're actually getting the position all the time. To display it, however, you need to change text attribute of your label:

from kivy.app import App
from kivy.uix.label import Label

class MousePos(App):
    def build(self):
        from kivy.core.window import Window
        self.label = Label()
        Window.bind(mouse_pos=lambda w, p: setattr(self.label, 'text', str(p)))
        return self.label

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

The code setattr(self.label, 'Mouse Position', str(p)) actually adds a new property to label object, called Mouse Position , that is never used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM