Kivy - 小部件在浮动布局中保持居中
Kivy - Widgets keep geting centred in floatlayout
我正在尝试使用 python 3.6.8 在 Kivy 2.0.0 中为雷达编写布局,在我尝试向我的布局中添加点之前一切看起来都很好。他们没有处于正确的位置,而是不断地居中。有人知道发生这种情况的原因吗?
我的代码基于这个旋转线示例:Example
有没有办法让这些点按其“pos”值定位?
Python代码
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.uix.button import Button
from kivy.animation import Animation
from kivymd.uix.textfield import MDTextField
from kivy.clock import Clock
import random
import math
class RadarDot(Widget):
random_color = [random.random(), random.random(), random.random(), 1]
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.auto_destruct, 4)
def auto_destruct(self, *args):
self.parent.remove_widget(self)
class RotatingLineWidget(Widget):
length = NumericProperty(10.0)
angle = NumericProperty()
class RadarApp(MDApp):
def build(self):
self.line = self.root.ids.line
def on_start(self):
pass
if __name__ == '__main__':
RadarApp().run()
Kivy 代码:
<RadarDot>:
canvas:
Color:
rgba: self.random_color
Ellipse:
size: 20, 20
<BorderCircle@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
circle: [0, 0, 300, -90 , 90]
<BottomLine@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
points: 300, 0, -300, 0
<BorderCircleMedium@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
circle: [0, 0, 200, -90 , 90]
<BorderCircleSmall@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
circle: [0, 0, 100, -90 , 90]
<RotateButton>:
<RotationInput>:
<RotatingLineWidget>:
canvas:
PushMatrix
Rotate:
angle: self.angle
axis: 0,0,1
Color:
rgba: 0.2, 1, 0.3, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
points: 0, 0, 0, self.length * 1
width: 2
PopMatrix
ScreenManager:
canvas.before:
Color:
rgba: 0, 0, 0, .8
Rectangle:
pos: self.pos
size: self.size
Screen:
size_hint: 1, 1
id: main_float
FloatLayout:
line: line
BorderCircle:
pos: root.size[0]/2, root.size[1]/2
BorderCircleMedium:
BorderCircleSmall:
BottomLine:
RotatingLineWidget:
main_float: main_float
id: line
length: 300
angle: 0
RadarDot:
pos: 200, 30
RadarDot:
pos: 500, 600
您没有为 Ellipse
提供 pos
,因此使用默认值 (0,0)
。尝试将 pos
添加到您的 <RadarDot>
规则:
<RadarDot>:
canvas:
Color:
rgba: self.random_color
Ellipse:
size: 20, 20
pos: self.pos
我正在尝试使用 python 3.6.8 在 Kivy 2.0.0 中为雷达编写布局,在我尝试向我的布局中添加点之前一切看起来都很好。他们没有处于正确的位置,而是不断地居中。有人知道发生这种情况的原因吗?
我的代码基于这个旋转线示例:Example
有没有办法让这些点按其“pos”值定位?
Python代码
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.uix.button import Button
from kivy.animation import Animation
from kivymd.uix.textfield import MDTextField
from kivy.clock import Clock
import random
import math
class RadarDot(Widget):
random_color = [random.random(), random.random(), random.random(), 1]
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.auto_destruct, 4)
def auto_destruct(self, *args):
self.parent.remove_widget(self)
class RotatingLineWidget(Widget):
length = NumericProperty(10.0)
angle = NumericProperty()
class RadarApp(MDApp):
def build(self):
self.line = self.root.ids.line
def on_start(self):
pass
if __name__ == '__main__':
RadarApp().run()
Kivy 代码:
<RadarDot>:
canvas:
Color:
rgba: self.random_color
Ellipse:
size: 20, 20
<BorderCircle@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
circle: [0, 0, 300, -90 , 90]
<BottomLine@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
points: 300, 0, -300, 0
<BorderCircleMedium@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
circle: [0, 0, 200, -90 , 90]
<BorderCircleSmall@Widget>:
canvas:
Color:
rgba: 1, 1, 1, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
width: 2
circle: [0, 0, 100, -90 , 90]
<RotateButton>:
<RotationInput>:
<RotatingLineWidget>:
canvas:
PushMatrix
Rotate:
angle: self.angle
axis: 0,0,1
Color:
rgba: 0.2, 1, 0.3, 1
Translate:
xy: self.pos[0], self.pos[1]
Line:
points: 0, 0, 0, self.length * 1
width: 2
PopMatrix
ScreenManager:
canvas.before:
Color:
rgba: 0, 0, 0, .8
Rectangle:
pos: self.pos
size: self.size
Screen:
size_hint: 1, 1
id: main_float
FloatLayout:
line: line
BorderCircle:
pos: root.size[0]/2, root.size[1]/2
BorderCircleMedium:
BorderCircleSmall:
BottomLine:
RotatingLineWidget:
main_float: main_float
id: line
length: 300
angle: 0
RadarDot:
pos: 200, 30
RadarDot:
pos: 500, 600
您没有为 Ellipse
提供 pos
,因此使用默认值 (0,0)
。尝试将 pos
添加到您的 <RadarDot>
规则:
<RadarDot>:
canvas:
Color:
rgba: self.random_color
Ellipse:
size: 20, 20
pos: self.pos