当前位置:网站首页>Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
2022-07-03 12:40:00 【知识大胖】
实战需求
我们有时需要在一个py文件里将代码和界面配置文件一起运行?
解决方案
我们可以使用Builder.load_string解决。
什么是Builder.load_string
在Builder中插入一个字符串并返回 kv 字符串的根小部件
load_string ( string , ** kwargs )
参数
rulesonly: bool, defaults to False
如果为 True,如果您在定义中有根小部件,则Builder将引发异常。filename: str, defaults to None
如果指定,则用于索引 kv 规则的文件名。
filename 参数可用于以与卸载 kv 文件相同的方式卸载 kv 字符串。这可以使用伪文件名来实现,例如:
Build.load_string("""
<MyRule>:
Label:
text="Hello"
""", filename="myrule.kv")
可以通过以下方式卸载:
Build.unload_file("myrule.kv")
实战代码
from kivy.app import App
from kivy.lang import Builder
# File: clock.kv
KV = '''
BoxLayout:
orientation: 'vertical'
Label:
text: '00:00:00'
'''
class ClockApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == '__main__':
ClockApp().run()
边栏推荐
- PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
- Cadre de logback
- 106. How to improve the readability of SAP ui5 application routing URL
- 【Colab】【使用外部数据的7种方法】
- Logback 日志框架
- C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
- Introduction to the implementation principle of rxjs observable filter operator
- 2022-01-27 use liquibase to manage MySQL execution version
- Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
猜你喜欢
DQL basic query
高效能人士的七个习惯
2022-02-11 heap sorting and recursion
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
Today's sleep quality record 77 points
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
MySQL
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
【Colab】【使用外部数据的7种方法】
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
随机推荐
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Red Hat Satellite 6:更好地管理服务器和云
SLF4J 日志门面
The difference between session and cookie
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Server coding bug
Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
Introduction to the implementation principle of rxjs observable filter operator
Logback log framework
SSH login server sends a reminder
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
elk笔记24--用gohangout替代logstash消费日志
JSP and filter
Task5: multi type emotion analysis
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
阿南的疑惑
2022-01-27 use liquibase to manage MySQL execution version
Luogup3694 Bangbang chorus standing in line
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator