【问题标题】:kivy change attributes of a class using code and not the kv filekivy 使用代码而不是 kv 文件更改类的属性
【发布时间】:2022-09-27 09:45:08
【问题描述】:

要更改类的属性,比如 Label 类的 font_size,我可以在 kv 文件中添加:

<Label>
    font_size: \"15sp\"

如何通过代码访问font_size 属性?

  • 看看ids
  • 我知道这些,它们非常适合小部件。但是,这不是一个类的小部件。

标签: python kivy kivy-language


【解决方案1】:

正如约翰在评论中所说,使用 ids 来识别您的标签。

.kv 文件

<MyBoxLayout>

    Label:
        id: mylabel
        font_size: '15dp'


    Button:
        on_release: root.change_font_size()

蟒蛇文件

class MyBoxLayout(BoxLayout):
   def change_font_size(self):
       self.ids.mylabel.font_size = '12dp'

显然,您可以用它做更多的事情,而不仅仅是更改一次大小,但这就是基本思想。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2020-07-28
    • 2018-05-16
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多