如何在 Kivy (Python) 中覆盖 2 个布局?
问题描述
我正在尝试制作一个具有背景网格和顶层交互元素的应用程序,我无法通过 python 覆盖第二层,所以就像标题所说的那样,有没有办法在 Kivy 中覆盖 2 个或多个布局在?
这就是我要找的东西
解决方案解决方案
设置第一层/布局的opacity
为0.5
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 ?
Here's what I am looking for
解决方案Solution
Set the opacity
of the first layer / layout to 0.5
Widget class » opacity
opacity
Opacity of the widget and all its children.
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 is a NumericProperty and defaults to 1.0.
Kivy Graphics Line » points
points: list
List of points in the format (x1, y1, x2, y2…)
Property for getting/settings points of the line
Warning
This will always reconstruct the whole graphics from the new points list. It can be very CPU expensive.
Kivy Graphics Line » circle
circle
Use this property to build a circle, without calculating the points. You can only set this property, not get it.
The argument must be a tuple of (center_x, center_y, radius, angle_start, angle_end, segments):
- 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.
Example
main.py - without 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 - using kv and 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)
'''))
Output
相关文章