<small id='gXJs1'></small><noframes id='gXJs1'>

    • <bdo id='gXJs1'></bdo><ul id='gXJs1'></ul>
      <tfoot id='gXJs1'></tfoot>
    1. <i id='gXJs1'><tr id='gXJs1'><dt id='gXJs1'><q id='gXJs1'><span id='gXJs1'><b id='gXJs1'><form id='gXJs1'><ins id='gXJs1'></ins><ul id='gXJs1'></ul><sub id='gXJs1'></sub></form><legend id='gXJs1'></legend><bdo id='gXJs1'><pre id='gXJs1'><center id='gXJs1'></center></pre></bdo></b><th id='gXJs1'></th></span></q></dt></tr></i><div id='gXJs1'><tfoot id='gXJs1'></tfoot><dl id='gXJs1'><fieldset id='gXJs1'></fieldset></dl></div>
      <legend id='gXJs1'><style id='gXJs1'><dir id='gXJs1'><q id='gXJs1'></q></dir></style></legend>

      如何在 Kivy 中自定义 style.kv

      时间:2023-10-09
        <i id='XTVe3'><tr id='XTVe3'><dt id='XTVe3'><q id='XTVe3'><span id='XTVe3'><b id='XTVe3'><form id='XTVe3'><ins id='XTVe3'></ins><ul id='XTVe3'></ul><sub id='XTVe3'></sub></form><legend id='XTVe3'></legend><bdo id='XTVe3'><pre id='XTVe3'><center id='XTVe3'></center></pre></bdo></b><th id='XTVe3'></th></span></q></dt></tr></i><div id='XTVe3'><tfoot id='XTVe3'></tfoot><dl id='XTVe3'><fieldset id='XTVe3'></fieldset></dl></div>
          <tbody id='XTVe3'></tbody>

        <small id='XTVe3'></small><noframes id='XTVe3'>

        <tfoot id='XTVe3'></tfoot>

            • <bdo id='XTVe3'></bdo><ul id='XTVe3'></ul>

              <legend id='XTVe3'><style id='XTVe3'><dir id='XTVe3'><q id='XTVe3'></q></dir></style></legend>
              • 本文介绍了如何在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:kivy.uix.screenmanager.ScreenManagerException:ScreenManager 只 下一篇:BoxLayout 中的两个 kivy 图像

                相关文章

                最新文章

                1. <small id='j6ajp'></small><noframes id='j6ajp'>

                2. <legend id='j6ajp'><style id='j6ajp'><dir id='j6ajp'><q id='j6ajp'></q></dir></style></legend>

                    <tfoot id='j6ajp'></tfoot>
                      <bdo id='j6ajp'></bdo><ul id='j6ajp'></ul>
                  1. <i id='j6ajp'><tr id='j6ajp'><dt id='j6ajp'><q id='j6ajp'><span id='j6ajp'><b id='j6ajp'><form id='j6ajp'><ins id='j6ajp'></ins><ul id='j6ajp'></ul><sub id='j6ajp'></sub></form><legend id='j6ajp'></legend><bdo id='j6ajp'><pre id='j6ajp'><center id='j6ajp'></center></pre></bdo></b><th id='j6ajp'></th></span></q></dt></tr></i><div id='j6ajp'><tfoot id='j6ajp'></tfoot><dl id='j6ajp'><fieldset id='j6ajp'></fieldset></dl></div>