Python 如何在Kivy中定制style.kv
根据,我可以通过创建一个本地风格的.kv文件来定制kivy应用程序的外观,该文件将被使用,而不是标准的。 因此,我通过修改按钮小部件的行为编辑了原始文件,如下所示:Python 如何在Kivy中定制style.kv,python,kivy,Python,Kivy,根据,我可以通过创建一个本地风格的.kv文件来定制kivy应用程序的外观,该文件将被使用,而不是标准的。 因此,我通过修改按钮小部件的行为编辑了原始文件,如下所示: <-Button,-ToggleButton>: canvas: Color: rgba: [1, 0, 0, 1] if self.state == 'normal' else [0, 0, 1, 1] Rectangle: pos
<-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.)
在运行时,style.kv的本地路径打印良好
非常感谢您的帮助 尽管网站上说你可以完全按照你所尝试的方式定制kivy,但它看起来并不管用。但是,只需使用kivy.lang.Builder加载修改后的style.kv,就可以让它正常工作。
例如:
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()
如果您将res_path更改为res_path=joindirnameabspath\uuuu file\uuuuu,自定义,数据会发生什么情况?没有更改,按钮行为与默认值一样。如果我在每个小部件重新定义之前使用-,则效果良好,如有人感兴趣,请添加破折号。
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()