如何在Kivy(Python)中覆盖2个布局? [英] How to overlay 2 layouts in Kivy (Python)?

查看:316
本文介绍了如何在Kivy(Python)中覆盖2个布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make an app with a background grid and a top layer of interactive elements, I am having trouble overlaying the second layer via python, so like the title says is there a way to overlay 2 or more layouts in Kivy in ?

这就是我要寻找的

推荐答案

解决方案

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

窗口小部件类»不透明

不透明

小部件及其所有子级的不透明性.

Opacity of the widget and all its children.

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

The opacity attribute controls the opacity of the widget and its children. Be careful, it’s a cumulative attribute: the value is multiplied by the current global opacity and the result is applied to the current context color.

...

opacity是 NumericProperty 和默认为1.0.

opacity is a NumericProperty and defaults to 1.0.

Kivy图形行»点

点:列表

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

List of points in the format (x1, y1, x2, y2…)

获取/设置直线点的属性

Property for getting/settings points of the line

警告

这将始终从新点重建整个图形 列表.这可能是非常昂贵的CPU.

This will always reconstruct the whole graphics from the new points list. It can be very CPU expensive.

Kivy图形线»圆

圆圈

使用此属性可以构建一个圆,而无需计算点. 您只能设置此属性,而不能获取它.

Use this property to build a circle, without calculating the points. You can only set this property, not get it.

该参数必须是(center_x,center_y,radius, angle_start,angle_end,线段):

The argument must be a tuple of (center_x, center_y, radius, angle_start, angle_end, segments):

  • center_x和center_y代表圆心
  • 半径代表圆的半径
  • (可选)angle_start和angle_end以度为单位.默认值为0和360.
  • (可选)线段是椭圆的精度.默认值是根据角度之间的范围计算的.
  • center_x and center_y represent the center of the circle
  • radius represent the radius of the circle
  • (optional) angle_start and angle_end are in degree. The default value is 0 and 360.
  • (optional) segments is the precision of the ellipse. The default value is calculated from the range between angle.

请注意,是否闭合圆圈取决于您.

Note that it’s up to you to close the circle or not.

示例

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)

'''))

输出

这篇关于如何在Kivy(Python)中覆盖2个布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆