【问题标题】:How to overlay 2 layouts in Kivy (Python)?如何在 Kivy (Python) 中覆盖 2 个布局?
【发布时间】:2019-03-11 19:26:15
【问题描述】:

我正在尝试制作一个具有背景网格和顶层交互元素的应用程序,我无法通过 python 覆盖第二层,所以就像标题所说的那样,有没有办法在 Kivy 中覆盖 2 个或多个布局在?

这就是我要找的东西

【问题讨论】:

    标签: python user-interface kivy overlay


    【解决方案1】:

    解决方案

    设置第一层/布局的opacity为0.5

    Widget class » opacity

    不透明度

    小部件及其所有子项的不透明度。

    opacity 属性控制小部件的不透明度及其 孩子们。注意,它是一个累积属性:值为 乘以当前的全局不透明度并将结果应用于 当前上下文颜色。

    ...

    不透明度是NumericProperty,默认为 1.0。

    Kivy Graphics Line » points

    点数:列表

    格式为 (x1, y1, x2, y2…) 的点列表

    获取/设置线的点的属性

    警告

    这将始终从新点重建整个图形 列表。它的 CPU 成本可能非常高。

    Kivy Graphics Line » circle

    圆圈

    使用此属性构建一个圆,而不计算点。 该属性只能设置,不能获取。

    参数必须是 (center_x, center_y, radius, 角度开始,角度结束,段):

    • center_x 和 center_y 代表圆心
    • radius代表圆的半径
    • (可选)angle_start 和 angle_end 以度为单位。默认值为 0 和 360。
    • (可选)段是椭圆的精度。默认值是根据角度之间的范围计算得出的。

    请注意,是否关闭圆圈由您决定。

    示例

    main.py - 没有 kv

    from kivy.base import runTouchApp
    from kivy.core.window import Window
    from kivy.uix.screenmanager import Screen
    from kivy.uix.boxlayout import BoxLayout
    from kivy.graphics import Color, Line
    from kivy.metrics import dp
    
    Window.clearcolor = (1, 1, 1, 1)
    
    
    class Overlay2Layouts(Screen):
    
        def __init__(self, **kwargs):
            super(Overlay2Layouts, self).__init__(**kwargs)
            self.size = Window.size
    
            layout1 = BoxLayout(opacity=0.5)
            with layout1.canvas:
                Color(1, 0, 0, 1)   # red colour
                Line(points=[self.center_x, self.height / 4, self.center_x, self.height * 3/4], width=dp(2))
                Line(points=[self.width * 3/ 4, self.center_y, self.width /4, self.center_y], width=dp(2))
    
            layout2 = BoxLayout()
            with layout2.canvas:
                Color(0, 0, 0, 1)   # black colour
                Line(circle=[self.center_x, self.center_y, 190], width=dp(2))
    
            self.add_widget(layout1)
            self.add_widget(layout2)
    
    
    if __name__ == "__main__":
        runTouchApp(Overlay2Layouts())
    

    main.py - 使用 kv 和 Python

    from kivy.lang import Builder
    from kivy.base import runTouchApp
    from kivy.core.window import Window
    
    Window.clearcolor = (1, 1, 1, 1)
    
    runTouchApp(Builder.load_string('''
    #:kivy 1.11.0
    
    Screen:
        BoxLayout:
            opacity: 0.5
            canvas.before:
                Color:
                    rgba: 1, 0, 0, 1
                Line:
                    width: dp(2.)
                    points: [self.center_x, self.height / 4, self.center_x, self.height * 3/4]
                Line:
                    width: dp(2.)
                    points: [root.width * 3/ 4, self.center_y, root.width /4, self.center_y]
        BoxLayout:
            canvas.before:
                Color:
                    rgba: 1, 0, 0, 1
                Line:
                    width: dp(2.)
                    circle: (root.center_x, root.center_y, 190)
    
    '''))
    

    输出

    【讨论】:

    • 谢谢,我正在寻找我之前应该提到的python中的等价物(编辑了问题)我正在尝试翻译您的示例。
    • 在 python 中无法弄清楚这一点:points: [root.width 任何想法?
    • points 是 Kivy 图形 Line 类的列表和一部分。这些点的格式为 (x1, y1, x2, y2…)。在示例中,root.width 指的是屏幕的宽度。
    • 我明白了,但是如果你在 python 中重新创建 .kv 文件,你需要以某种方式引用 root,到目前为止我还没有找到正确的绑定方法。
    • 实现一个class Overlay2Layouts(Screen):,这将是根。使用self 引用它。请参考更新后的帖子,例如不使用 kv。
    【解决方案2】:

    为了补充python中的答案,当窗口改变大小时,叠加层没有调整大小,所以这是我的解决方案:

    import kivy
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.gridlayout import GridLayout
    from kivy.graphics import Color, Line, Ellipse, Rectangle
    from kivy.metrics import dp
    
    
    class RootWidget(BoxLayout):
    
        def __init__(self, *args, **kwargs):
            BoxLayout.__init__(self, *args, **kwargs)
            self.bind(pos=self.draw)
            self.bind(size=self.draw)
            self.layout1 = BoxLayout(opacity=0.3)
            self.layout2 = BoxLayout()
            self.add_widget(self.layout1)
            self.add_widget(self.layout2)
    
        def draw(self, *args):
            with self.canvas.before:
                Color(1,1,.5,1)
                self.bg = Rectangle(pos=self.pos, size=self.size)
            self.layout1.canvas.clear()
            with self.layout1.canvas:
                Color(1, 0, 0, 1)   # red colour
                Line(points=[self.center_x, self.height / 4, self.center_x, self.height * 3/4], width=dp(2))
                Line(points=[self.width * 3/ 4, self.center_y, self.width /4, self.center_y], width=dp(2))
            self.layout2.canvas.clear()
            with self.layout2.canvas:
                Color(0, 0, 0, 1)   # black colour
                Line(circle=[self.center_x, self.center_y, 190], width=dp(2))
    
    
    class Overlays_3(App):
        title = "Overlays_3"
    
        def build(self):
            return RootWidget()
    
    
    if __name__ == "__main__":
        Overlays_3().run()
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多