python - kivy anchor 布局不锚定?

标签 python layout kivy

我试图让名为“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

这是它当前的样子:(为了说明目的,按下了其中一个切换按钮) Number not lining up

最佳答案

在您的示例中,标签与它们所属的 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

结果:

enter image description here

关于python - kivy anchor 布局不锚定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546517/

相关文章:

python - __init__.py imports 还暴露了我使用的模块,而不仅仅是我自己的类

python - CoreOS构建Docker镜像(CP100A培训)

python - 使用 scikit python 进行自变量线性回归

CSS 一张固定宽度的图像和一张占据空白空间的图像

android - 在线性布局中拉伸(stretch)图像按钮以使用可用空间

python - 根据 child 的大小动态调整小部件的大小

android - 使用 Anaconda Python 3.6 在 Windows 7 上安装 Kivy

python - Pandas:计算数据帧每列的一组n个值的平均值

python - json 存储未正确更新值 kivy

java - 在java swing中调整窗口大小时如何使按钮粘在一个地方?