Python|基维 .kv 文件
就像我们在Python kivy 中编写应用程序一样,在同一代码上编写所有东西会使代码变得一团糟,其他人很难理解这一点。编写大型代码也使得难以维护小部件树的构造和显式绑定声明。
KV 语言允许我们以声明方式创建自己的小部件树,并将小部件属性彼此绑定或以自然方式绑定到回调。
???????? Kivy Tutorial – Learn Kivy with Examples.
如何加载kv文件:
有 2 种方法可以将 .kv 文件加载到代码或应用程序中
- 按名称约定方法-
在编写代码时,我们将创建 App 类。对于此方法,文件名和应用程序类名相同,并将 kv 文件保存为 appclassname.kv。
Kivy 查找与您的 App 类同名的小写 Kv 文件,如果它以 'App' 结尾,则减去“App”,例如:
classnameApp ---> classname.kv
如果此文件定义了一个 Root Widget,它将附加到 App 的 root 属性并用作应用程序小部件树的基础。
下面给出了如何在 kivy 中使用 .kv 文件的示例代码:
Python3
# code how to use .kv file in kivy
# import kivy module
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
# not compulsory to write it
kivy.require('1.9.1')
# define the App class
# and just pass rest write on kvfile
# not necessary to pass
# can also define function in it
class kvfileApp(App):
pass
kv = kvfileApp()
kv.run()
Python3
Label:
text:
('[b]Hello[/b] [color = ff0099]World[/color]\n'
'[color = ff0099]Hello[/color] [b]World[/b]\n'
'[b]Hello[/b] [color = ff0099]World:):)[/color]')
markup: True
font_size: '64pt'
Python3
# code to use the .kv file as a string in the main file
# code how to use .kv file in kivy
# import kivy module
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# it is to import Builder
from kivy.lang import Builder
# this restrict the kivy version i.e
# below this kivy version you cannot use the app or software
# not compulsory to write it
kivy.require('1.9.1')
# building kv file as string
kvfile = Builder.load_string("""
Label:
text:
('[b]Hello[/b] [color = ff0099]World[/color]\\n'
'[color = ff0099]Hello[/color] [b]World[/b]\\n'
'[b]Hello[/b] [color = ff0099]World:):)[/color]')
markup: True
font_size: '64pt'
""")
# define the App class
# and just pass rest write on kvfile
# not necessary to pass
# can also define function in it
class kvfileApp(App):
def build(self):
return kvfile
kv = kvfileApp()
kv.run()
- .kv 文件代码以与应用程序类相同的名称保存 -
Python3
Label:
text:
('[b]Hello[/b] [color = ff0099]World[/color]\n'
'[color = ff0099]Hello[/color] [b]World[/b]\n'
'[b]Hello[/b] [color = ff0099]World:):)[/color]')
markup: True
font_size: '64pt'
输出:
- 生成器方法-
要使用此方法,您首先必须通过编写导入 Builder
from kivy.lang import builder
现在通过构建器,您可以直接将整个文件加载为字符串或文件。通过这样做将 .kv 文件加载为文件:
Builder.load_file('.kv/file/path')
或者,对于加载,kv 文件作为字符串:
Builder.load_string(kv_string)
Python3
# code to use the .kv file as a string in the main file
# code how to use .kv file in kivy
# import kivy module
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# it is to import Builder
from kivy.lang import Builder
# this restrict the kivy version i.e
# below this kivy version you cannot use the app or software
# not compulsory to write it
kivy.require('1.9.1')
# building kv file as string
kvfile = Builder.load_string("""
Label:
text:
('[b]Hello[/b] [color = ff0099]World[/color]\\n'
'[color = ff0099]Hello[/color] [b]World[/b]\\n'
'[b]Hello[/b] [color = ff0099]World:):)[/color]')
markup: True
font_size: '64pt'
""")
# define the App class
# and just pass rest write on kvfile
# not necessary to pass
# can also define function in it
class kvfileApp(App):
def build(self):
return kvfile
kv = kvfileApp()
kv.run()
输出: