我试图让名为“number”的标签直接显示在椭圆的中心上方。我尝试过将两者放在相同的布局、不同的布局、AnchorLayouts和RelativeLayouts中,但我一直不知道如何做到这一点。
这是我的 python 的测试版本,显示了相同的问题:
class BugTester(FloatLayout):
def __init__(self):
super().__init__()
for i in range(1,6):
temp = GamePiece(5, "Red")
temp.pos = (i*100,i*200)
self.add_widget(temp)
class BugTestingApp(App):
def build(self):
return BugTester()
class GamePiece(ToggleButton):
def __init__(self, number, color, **kwargs):
self.number = number
self.player_color = color
self.background_normal = "5.png"
super(GamePiece, self).__init__()
if __name__ == '__main__':
BugTestingApp().run()
还有我的 kivy :
<BugTester>:
Button:
background_disabled_normal: "background.jpg"
disabled: True
<GamePiece>:
id: gamepiece
size_hint:(None, None)
group: self.player_color
border: (0,0,0,0)
AnchorLayout:
id: layout
center: root.center
on_size: self.size = root.size
anchor_x: "left"
anchor_y: "bottom"
canvas:
Color:
rgba: 0,0,0,1
Ellipse:
#this is the ellipse in question
id: circle
size: (root.width/2.5, root.height/3)
#pos: -15, -20
pos: root.pos
Label:
#this is the label I want centered over the ellipse
id: number
text: str(root.number)
color: (1,1,1,1)
font_size: root.height/4
bold: True
#pos: root.pos
最佳答案
在您的示例中,标签与它们所属的 anchor 布局一样大,因此您无法移动它们。
如果您希望它们具有其他大小,请禁用 size_hint
,并使用固定的大小
(例如,与省略号一样大):
Label:
#this is the label I want centered over the ellipse
size_hint: None, None
size: (root.width/2.5, root.height/3)
id: number
text: str(root.number)
color: (1,1,1,1)
font_size: root.height/4
bold: True
#pos: root.pos
结果:
关于python - kivy anchor 布局不锚定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546517/