我能绕线千伏吗?

2024-03-28 18:56:44 发布

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

我有一些.kv,我经常更改,它有多个相同的部分(但是具有不同的id)。例如:

GridLayout:
    cols: 1

    Button:
        id: button1
        text: 'Button 1'
        on_press: app.buttonpressed(1)

    Button:
        id: button2
        text: 'Button 2'
        on_press: app.buttonpressed(2)

    Button:
        id: button3
        text: 'Button 3'
        on_press: app.buttonpressed(3)

……等等。有没有一种方法可以定义一次按钮并重用它?很显然,我可以用python来完成这项工作,即不使用.kv,但我希望将所有内容都保存在.kv中


Tags: 方法textidapp定义onbuttonpress
1条回答
网友
1楼 · 发布于 2024-03-28 18:56:44

您可以使用kivy动态类(以及旧版本的模板)。在

下面是文档(https://kivy.org/docs/guide/lang.html#dynamic-classes)中的一个示例。在

<CustomButton@Button>:
    text_size: self.size
    font_size: '25sp'
    markup: True

<MyWidget>:
    CustomButton:
        text: "Hello world, watch this text wrap inside the button"
    CustomButton:
        text: "Even absolute is relative to itself"
    CustomButton:
        text: "repeating the same thing over and over in a comp = fail"
    CustomButton:

相关问题 更多 >