从Kivy中的其他小部件获取属性

2024-10-01 07:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在不同的小部件中使用属性? 我有这个密码:

Python文件

class Control(ToggleButton):
    width_multiplier = NumericProperty(1)


class MainScreen(StackLayout):
    pass


class HomeControl(App):

    def build(self):
        return MainScreen()


if __name__ == '__main__':
    HomeControl().run()

KV文件

<Control>:
    size_hint: None,None
    height: '100dp'
    width: self.width_multiplier * self.height + (self.width_multiplier - 1) * spacing
    halign: 'center'

<MainScreen>:
    orientation: 'lr-tb'
    spacing: '10dp'
    padding: '15dp'

    Control:
        text: 'Button'

    Control:
        text: 'Long Button'
        width_multiplier: 2

Control小部件的宽度应该是width_multiplier乘以小部件的高度加上MainScreen子部件之间的间距。如何在Control角色中使用MainScreen的spacing属性? 我是新来基维,所以这可能是一个愚蠢的问题,但我希望有人能帮助我。你知道吗


Tags: 文件textselfnone属性部件buttonwidth
1条回答
网友
1楼 · 发布于 2024-10-01 07:37:30

在您的情况下,可以使用parent属性

<Control>:
    size_hint: None,None
    height: '100dp'
    width: self.width_multiplier * self.height + (self.width_multiplier - 1) * self.parent.spacing[0]  < - 
    halign: 'center'

相关问题 更多 >