How to customize style.kv in Kivy(如何在 Kivy 中自定义 style.kv)
问题描述
根据 Kivy Doc,我可以通过创建将使用而不是标准的本地 style.kv 文件来自定义 kivy 应用程序外观.所以我编辑了原始文件,通过修改按钮小部件的行为,如下所示:
According to Kivy Doc, I can customize a kivy app look, by creating a local style.kv file that will be use instead of standard. So I edited the original file, by modifying, the behavior of the Button widget like this :
<-Button,-ToggleButton>:
canvas:
Color:
rgba: [1, 0, 0, 1] if self.state == 'normal' else [0, 0, 1, 1]
Rectangle:
pos: self.pos
size: self.size
Color:
rgba: 1, 1, 1, 1
Rectangle:
texture: self.texture
size: self.texture_size
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.)
我希望按钮背景在单击时变为红色并变为蓝色.但是什么也没发生,并且应用了默认行为.
I was hoping that buttons background's become red and change to blue when clicked. But nothings happen, and the default behavior was applied.
这是我的主文件的内容
from os.path import abspath, dirname, join
from kivy.app import App
from kivy.resources import resource_add_path, resource_find
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class MainLayout(BoxLayout):
def __init__(self, **kwargs):
super(MainLayout, self).__init__(**kwargs)
self.add_widget(Button(text="Button1"))
self.add_widget(Button(text="Button2"))
class MainApp(App):
def build(self):
return MainLayout()
if __name__ == '__main__':
res_path = join(dirname(abspath(__file__)), "custom")
resource_add_path(res_path)
print("find ", resource_find("data/style.kv"))
MainApp().run()
在运行时,style.kv 的本地路径打印良好.
At runtime, the local path of the style.kv is well printed.
非常感谢所有帮助!
推荐答案
即使 documentation 说您可以完全按照您尝试的方式自定义 kivy,但它看起来不起作用.但是,您可以通过使用 kivy.lang.Builder
加载修改后的 style.kv
来使其工作.例如:
Even though the documentation says that you can customize kivy exactly the way you have attempted, it does not look like it works. However, you can get it to work by just loading your modified style.kv
using kivy.lang.Builder
.
For example:
from kivy.lang import Builder
Builder.load_string('''
<-Button,-ToggleButton>:
canvas:
Color:
rgba: [1, 0, 0, 1] if self.state == 'normal' else [0, 0, 1, 1]
Rectangle:
pos: self.pos
size: self.size
Color:
rgba: 1, 1, 1, 1
Rectangle:
texture: self.texture
size: self.texture_size
pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.)
''')
from os.path import abspath, dirname, join
from kivy.app import App
from kivy.resources import resource_add_path, resource_find
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class MainLayout(BoxLayout):
def __init__(self, **kwargs):
super(MainLayout, self).__init__(**kwargs)
self.add_widget(Button(text="Button1"))
self.add_widget(Button(text="Button2"))
class MainApp(App):
def build(self):
return MainLayout()
if __name__ == '__main__':
MainApp().run()
这篇关于如何在 Kivy 中自定义 style.kv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Kivy 中自定义 style.kv


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01