在Kivy中获得widget的全球位置?

2024-09-26 17:55:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我用RelativeLayout定位了一个相对于屏幕大小的图像。如何获取该图像的全局(/窗口)位置,以便检测触摸坐标是否与图像重叠

这是我目前的想法:

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

如您所见,我试图使用to_parent来获取相对于父对象的位置。然而,我认为它不起作用

我也尝试过使用AnchorLayout,也遇到过类似的问题

注意:虽然我知道我可以使用window_size,但我需要一个更健壮的解决方案,可以处理更复杂的情况


Tags: thetofrompos图像imageimportapp

热门问题