当前位置:网站首页>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()
边栏推荐
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter V exercises]
- Introduction to the implementation principle of rxjs observable filter operator
- CVPR 2022 图像恢复论文
- 开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
- 2022-02-10 introduction to the design of incluxdb storage engine TSM
- luoguP3694邦邦的大合唱站队
- Sword finger offer 14- ii Cut rope II
- Anan's doubts
- Task6: using transformer for emotion analysis
- 【Colab】【使用外部数据的7种方法】
猜你喜欢
Annotation and reflection
我的创作纪念日:五周年
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
2022-02-11 heap sorting and recursion
Servlet
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
道路建设问题
OpenHarmony应用开发之ETS开发方式中的Image组件
rxjs Observable filter Operator 的实现原理介绍
随机推荐
Create a dojo progress bar programmatically: Dojo ProgressBar
有限状态机FSM
File uploading and email sending
【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
对业务的一些思考
[exercise 5] [Database Principle]
Flink SQL knows why (13): is it difficult to join streams? (next)
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
Annotation and reflection
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]
2022-02-10 introduction to the design of incluxdb storage engine TSM
Kotlin - 改良装饰者模式
Luogup3694 Bangbang chorus standing in line
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
剑指 Offer 16. 数值的整数次方
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
2022-01-27 redis cluster cluster proxy predixy analysis
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
道路建设问题