gpt4 book ai didi

kivy - Kv 语言中的变量 (kivy)

转载 作者:行者123 更新时间:2023-12-02 06:33:03 27 4
gpt4 key购买 nike

我的 Kivy 语言文件有许多 font_size 属性,所有属性都具有相同的值,是否可以在 KV lang 中分配变量?
当前 KV 文件示例:

#User ID
Label:
text: 'User ID'
font_size: 20
text_size: self.size

TextInput:
id: userid
font_size: 20


#User PW
Label:
text: 'Password'
font_size: 20
text_size: self.size

TextInput:
id: password
password: True
font_size: 20

Button:
text: 'Login'
font_size: 20

是否可以像这样设置它:
#User ID
@fs: 20
Label:
text: 'User ID'
font_size: fs
text_size: self.size

TextInput:
id: userid
font_size: fs


#User PW
Label:
text: 'Password'
font_size: fs
text_size: self.size

TextInput:
id: password
password: True
font_size: fs

Button:
text: 'Login'
font_size: fs

通过这样做,我只能通过更改 FS 变量值来立即更改字体大小,此外,类似的解决方案可能会帮助我更快地创建基于主题的文件。谢谢你。

最佳答案

I would be able to change the font size at once only by changing the FS variable value,



您可以使用 #:set name value 设置一个值,但这并不是您想要的。由于您希望变量更新,您应该使用 kivy 属性,以便事件系统为您处理它。

在这种情况下,由于您希望许多不同的东西依赖于这样的大小,例如,您可以使用应用程序类的属性。
class YourApp(App):
font_size = NumericProperty(20)

然后在 kv
font_size: app.font_size

对 App 实例的 font_size 的任何更改都将自动传播到这些 kv 规则。

关于kivy - Kv 语言中的变量 (kivy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28255695/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com