我在Kivy尝试将图像添加到Scatter时遇到了很多问题。我希望图像首先出现与boxlayout相同的高度。然后我希望能够移动和缩放它(这确实有效)。当我运行代码时,图像显示非常小。如果窗口的大小改变,我还希望图像自己调整大小。我是kivy的新手,所以任何帮助都会很棒。谢谢!
<MyGridLayout>:
rows: 1
BoxLayout:
id:layout1
orientation: 'vertical'
BoxLayout:
id: box1
size_hint : [1,0.5]
StencilView:
ScatterLayout:
center_x: box1.center_x
center_y: box1.center_y
Image:
source: 'histo_test.png'
size_hint_y: None
size_hint_x: None
width: self.parent.width
height: self.parent.width/self.image_ratio
center: self.parent.center
allow_stretch: True
keep_ratio: True
BoxLayout:
size_hint : [1,0.5]
id:box2
StencilView:
ScatterLayout:
center_x: box2.center_x
center_y: box2.center_y
Image:
source: 'flower.png'
size_hint_y: None
size_hint_x: None
width: self.parent.width
height: self.parent.width/self.image_ratio
center: self.parent.center
allow_stretch: True
keep_ratio: True
答案 0 :(得分:0)
图像的大小必须是StencilView的大小,所以:
size: stencil.width, stencil.height
或
width: stencil.width
height: stencil.width/self.image_ratio
(模板是StencilView的ID)
ScatterLayout的大小必须是图像的大小:
size: my_image.size
(my_image是图片的ID)
您还可以使用StencilView的位置设置ScatterLayout的位置:
pos: stencil.pos
而不是:
center_x: box1.center_x
center_y: box1.center_y
观看this video了解详情。
我编写的代码几乎完全相同
<MyGridLayout>:
orientation: 'vertical'
StencilView:
id: stencil1
Scatter:
pos: stencil1.pos
size: my_image1.size
Image:
id: my_image1
size: stencil1.width, stencil1.height
source: 'dog.jpg'
allow_stretch: True
keep_ratio: False
StencilView:
id: stencil2
Scatter:
pos: stencil2.pos
size: my_image2.size
Image:
id: my_image2
size: stencil2.width, stencil2.height
source: 'flower.jpg'
allow_stretch: True
keep_ratio: False
对不起,如果我的英语不清楚