我希望能够在画布上创建多个形状并使它们均匀地间隔开。我还想让形状在到达屏幕末端时开始新的一行。
这里有我现在的代码:
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
答案 0 :(得分:1)
pos[0]
,pos[1]
,size[0]
,size[1]
rect.size和new_pos中的0和1做什么?
pos
小部件的位置。
pos是(ReferenceListProperty,x)中的y 属性。
pos
包含小部件位置的x和y坐标。 pos[0]
表示x坐标,pos[1]
表示y坐标。
size
小部件的大小。
size是(ReferenceListProperty,width)中的height 属性。
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]