当前位置:网站首页>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()
边栏推荐
- Introduction to the implementation principle of rxjs observable filter operator
- Today's sleep quality record 77 points
- Setting up Oracle datagurd environment
- Elk note 24 -- replace logstash consumption log with gohangout
- 106. How to improve the readability of SAP ui5 application routing URL
- 71 articles on Flink practice and principle analysis (necessary for interview)
- [data mining review questions]
- When we are doing flow batch integration, what are we doing?
- Dojo tutorials:getting started with deferrals source code and example execution summary
- 解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
猜你喜欢
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]
Some thoughts on business
elk笔记24--用gohangout替代logstash消费日志
正则表达式
Sitescms v3.1.0 release, launch wechat applet
Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
随机推荐
STM32 and motor development (from MCU to architecture design)
有限状态机FSM
February 14, 2022, incluxdb survey - mind map
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Detailed explanation of multithreading
MySQL constraints
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
Servlet
OpenHarmony应用开发之ETS开发方式中的Image组件
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
Sitescms v3.0.2 release, upgrade jfinal and other dependencies
剑指 Offer 11. 旋转数组的最小数字
Create a dojo progress bar programmatically: Dojo ProgressBar
Introduction to the implementation principle of rxjs observable filter operator
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
Image component in ETS development mode of openharmony application development
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包