[英]How do I create multiple shapes relative to each other in kivy?
我希望能够在画布上创建多个形状并使它们均匀地间隔开。 我还想让形状在到达屏幕末端时开始新的一行。
这是我现在拥有的代码:
def draw_streak(self, obj):
name = obj.text
can = self.root.get_screen("three")
with open("streak.json", "r") as file:
read = json.load(file)
for key in read.keys():
if key == name:
with open("streak.json", "r+") as f:
data = json.load(f)
get_score = data.get(key, {}).get('score')
for x in range(get_score):
with can.ids.my_box.canvas:
Color(0, 1, 0, .75, mode='rgba')
Rectangle(pos=can.pos, size=(30,30))
如果get_score
为10,那么我想绘制10个矩形。 现在,当我运行程序时,只绘制了一个矩形。 我相信正在画多个,但它们彼此重叠。
编辑
这是kv代码:
<ScreenThree>
id: screen_three
name: "three"
on_leave: my_box.canvas.clear()
on_leave: selected_streak.canvas.clear()
...
BoxLayout:
id: my_box
orientation: "vertical"
my_box
只是BoxLayout的ID
pos[0]
, pos[1]
, size[0]
, size[1]
rect.size和new_pos中的0和1是做什么的?
pos
小部件的位置。
pos是( x , y )属性的ReferenceListProperty 。
pos
包含小部件位置的x和y坐标。 pos[0]
表示x坐标,而pos[1]
表示y坐标。
size
小部件的大小。
size是( width , height )属性的ReferenceListProperty 。
size
包含小部件的宽度和高度。 size[0]
表示宽度, size[1]
表示高度。
为防止矩形重叠,请保存起始位置,并在绘制每个矩形后对其进行递增。
以下片段对角线添加矩形。
new_pos = can.pos
for x in range(get_score):
with can.ids.my_box.canvas:
Color(0, 1, 0, .75, mode='rgba')
rect = Rectangle(pos=new_pos, size=(30, 30))
new_pos[0] += rect.size[0]
new_pos[1] += rect.size[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.