Python 在kivy中,如何将小部件放置在图像上方以及相对于图像的位置?

Python 在kivy中,如何将小部件放置在图像上方以及相对于图像的位置?,python,kivy,Python,Kivy,我目前正在为学生开发一个应用程序,它描述了各种实际实验。 该应用程序由几个手风琴项目组成。 问题出现在其中一个,我想嵌入一个图像,它保持其纵横比,但适应尽可能大。为了解释部分图像,我想在有趣的设备/对象上放置半透明按钮,这些设备/对象在on_发布时以文本形式提供信息 通过沿x或y方向拉伸应用程序窗口以及不允许拉伸图像的事实,窗口的上下或左右两侧存在不属于实际图像的区域 如何根据图像的当前大小缩放和定位按钮 我试着使用RelativeLayout,但按钮似乎面向整个窗口,我不明白。 我也尝试过使用

我目前正在为学生开发一个应用程序,它描述了各种实际实验。 该应用程序由几个手风琴项目组成。 问题出现在其中一个,我想嵌入一个图像,它保持其纵横比,但适应尽可能大。为了解释部分图像,我想在有趣的设备/对象上放置半透明按钮,这些设备/对象在on_发布时以文本形式提供信息

通过沿x或y方向拉伸应用程序窗口以及不允许拉伸图像的事实,窗口的上下或左右两侧存在不属于实际图像的区域

如何根据图像的当前大小缩放和定位按钮

我试着使用RelativeLayout,但按钮似乎面向整个窗口,我不明白。 我也尝试过使用ids,但我不知道如何有效地使用它

以下是一些当前代码:

class LaserApp(应用程序):
通过
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#Config.set('图形'、'全屏'、'自动')
Config.set('graphics'、'fullscreen'、'0')
Config.set('图形'、'窗口状态'、'窗口化')
Config.write()
LaserApp().run()
欢迎任何意见和帮助。 非常感谢


旁白:请原谅这个不好的描述。

对于你的问题,听起来你想要这样的东西?:

_____________
|   |   |   |
_____________
|   |   |   |
_____________
|   |   |   |
_____________
您必须使用以下选项创建多个按钮:

RelativeLayout:
    size_hint: 1,1
    pos: self.pos
    Button:
        background_color: 0,0,0,.25 #rgba only, this makes the button semi-transparent
        pos_hint: {'x': .66, 'y': 0}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .33, 'y': 0}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': 0, 'y': 0}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .66, 'y': .33}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .33, 'y': .33}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': 0, 'y': .33}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .66, 'y': .66}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .33, 'y': .66}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': 0, 'y': .66}
        size_hint: .33, .33
        on_release: print/function

只要你有一个带有
size\u提示:1,1
pos:self.pos
的布局附加到你的图像,这将把布局绑定到你的图像小部件的相同大小和位置。示例代码中的按钮应填充1/9大小的平铺,因为9以.33的增量设置。如果您想要更多,只需使用正确的大小提示和位置提示值添加更多。如果RelativeLayout是一个问题,请尝试使用FloatLayout或BoxLayout,FloatLayout类似于放置空白画布,您可以通过垂直或水平填充子项来填充此小部件,这取决于您如何设置
方向:
。如果这没有完全回答您的问题,或者您需要更多帮助,请告诉我。

对于您的问题,听起来您想要这样的东西?:

_____________
|   |   |   |
_____________
|   |   |   |
_____________
|   |   |   |
_____________
您必须使用以下选项创建多个按钮:

RelativeLayout:
    size_hint: 1,1
    pos: self.pos
    Button:
        background_color: 0,0,0,.25 #rgba only, this makes the button semi-transparent
        pos_hint: {'x': .66, 'y': 0}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .33, 'y': 0}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': 0, 'y': 0}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .66, 'y': .33}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .33, 'y': .33}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': 0, 'y': .33}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .66, 'y': .66}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': .33, 'y': .66}
        size_hint: .33, .33
        on_release: print/function
    Button:
        background_color: 0,0,0,.25
        pos_hint: {'x': 0, 'y': .66}
        size_hint: .33, .33
        on_release: print/function

只要你有一个带有
size\u提示:1,1
pos:self.pos
的布局附加到你的图像,这将把布局绑定到你的图像小部件的相同大小和位置。示例代码中的按钮应填充1/9大小的平铺,因为9以.33的增量设置。如果您想要更多,只需使用正确的大小提示和位置提示值添加更多。如果RelativeLayout是一个问题,请尝试使用FloatLayout或BoxLayout,FloatLayout类似于放置空白画布,您可以通过垂直或水平填充子项来填充此小部件,这取决于您如何设置
方向:
。如果这没有完全回答您的问题,或者您需要更多帮助,请告诉我。

嘿,谢谢您的回答。我也尝试过类似的想法。我在图片上下各放置了一个按钮,以占据图片上方的空间。不幸的是,这种方法不起作用。我认为这是因为纹理的处理方式。在尝试时,我注意到图像立即接管了背景(窗口)的参数(高度/宽度),因此直接与图像对齐可能是不可能的。我很感谢你的进一步建议,谢谢你的回答。我也尝试过类似的想法。我在图片上下各放置了一个按钮,以占据图片上方的空间。不幸的是,这种方法不起作用。我认为这是因为纹理的处理方式。在尝试时,我注意到图像立即接管了背景(窗口)的参数(高度/宽度),因此直接与图像对齐可能是不可能的。我很感激有更多的想法