[Python] Get global position of widget in Kivy?

Discussão em 'Python' iniciado por Stack, Agosto 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have an image located relative to the screen size with RelativeLayout. How can I get the global (/window) position of that image, so that I can detect if touch coordinates overlap the image?

    this is my current implementation:

    from kivy.app import App

    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.image import Image
    from kivy.uix.relativelayout import RelativeLayout

    class mainApp(App):
    def build(slef):

    appRoot = FloatLayout()
    relPos = RelativeLayout()

    button = Image()
    relPos.pos_hint = {"right":1,"top":1.5}

    relPos.add_widget(button)

    #attempting to print the coordinates of the Image in the window
    print(relPos.to_parent(*button.pos))

    return relPos

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


    As you can see, I'm attempting to use to_parent to get the position relative to the parent. However, I don't think it works pos_hint.

    I've also tried using AnchorLayout, and have had similar issues

    Note: while I know I can use the window_size, along with a bit of math, I would like a more robust solution that can handle more complex situations

    Continue reading...

Compartilhe esta Página