当前位置:网站首页>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()
边栏推荐
- The 35 required questions in MySQL interview are illustrated, which is too easy to understand
- Introduction to the implementation principle of rxjs observable filter operator
- 剑指 Offer 12. 矩阵中的路径
- 阿南的疑惑
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- 2022-02-11 practice of using freetsdb to build an influxdb cluster
- Red Hat Satellite 6:更好地管理服务器和云
- Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
- Cadre de logback
- 【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
猜你喜欢
![[colab] [7 methods of using external data]](/img/cf/07236c2887c781580e6f402f68608a.png)
[colab] [7 methods of using external data]

Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
![【R】 [density clustering, hierarchical clustering, expectation maximization clustering]](/img/a2/b287a5878761ee22bdbd535cae77eb.png)
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]](/img/8b/bef94d11ac22e3762a570dab3a96fa.jpg)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]

Detailed explanation of multithreading

【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
![[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]](/img/b4/3442c62586306b4fceca992ce6294a.png)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]

Road construction issues

sitesCMS v3.1.0发布,上线微信小程序

Seven habits of highly effective people
随机推荐
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
剑指 Offer 12. 矩阵中的路径
Mysql database basic operation - regular expression
Convolution emotion analysis task4
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
道路建设问题
Elk note 24 -- replace logstash consumption log with gohangout
Reptile
regular expression
父亲和篮球
Kotlin - 改良装饰者模式
Logback log framework
MapReduce实现矩阵乘法–实现代码
2022-02-14 incluxdb cluster write data writetoshard parsing
2022-01-27 use liquibase to manage MySQL execution version
MySQL_ JDBC
luoguP3694邦邦的大合唱站队
Sitescms v3.0.2 release, upgrade jfinal and other dependencies
Sword finger offer 14- I. cut rope
Sword finger offer 17 Print from 1 to the maximum n digits