【问题标题】:How to change canvas colour in Kivy using id:?如何使用 id: 更改 Kivy 中的画布颜色?
【发布时间】:2021-08-17 13:12:22
【问题描述】:

环顾四周,似乎找不到任何关于通过引用 id: 在 Kivy 中更改画布颜色的可靠信息。我的目标是为我的应用程序创建一个明暗主题,其中用户在应用程序设置中更改它。这是我目前的 .kv 代码:

themescreen.kv

<ThemeScreen>:
    id: theme_canvas
    canvas:
        Color:
            rgb: utils.get_color_from_hex("#161D39")
        Rectangle:
            size: self.size
            pos: self.pos
    FloatLayout:
        RoundedButton:
            id: dark_theme
            text: "Dark"
            pos_hint:  {"top": .5, "center_x": .5}
            size_hint: .38, .08
            padding: 20, 20
            opacity: 1 if self.state == 'normal' else .5
            on_release:
                app.change_theme("#000523")
        RoundedButton:
            id: light_theme
            text: "Light"
            pos_hint:  {"top": .3, "center_x": .5}
            size_hint: .38, .08
            padding: 20, 20
            opacity: 1 if self.state == 'normal' else .5
            on_release:
                app.change_theme("#C4CCFF")

所以我要做的是运行将我的十六进制颜色更改为适当主题的函数。下面是我的 .py 函数。

    def change_theme(self, theme):
        theme_canvas = self.root.ids["theme_screen"].ids["theme_canvas"]
        theme_canvas.rgb = utils.get_color_from_hex(theme)

我尝试将 id: 放置在不同的位置,我尝试将 rgb 颜色纯粹放在 FloatLayout 区域中,并在画布中使用 self.color 等,然后调用该 id: 代替。目前,通过此设置,我收到错误消息: theme_canvas =self.root.ids["theme_screen"].ids["theme_canvas"] KeyError: 'theme_canvas' 我已经厌倦了从函数中删除theme_canvas

请问有什么办法可以吗?我只是希望能够通过我的函数更改 rgb 颜色。请帮忙。

【问题讨论】:

    标签: python function canvas colors kivy


    【解决方案1】:

    您的问题有替代解决方案

    由于您的目标是在运行时更改主题,您可以使用 kivymd(kivy 的材料设计库)来完成此操作

    你可以在App类中使用这个来调整主题

    #light theme
    
    self.theme_cls.theme_style = "Light"
    
    #Dark theme
    self.theme_cls.theme_style = "Dark"
    

    这里是这个案例的一个实现

    from kivymd.app import MDApp
    from kivy.lang import Builder
    
    Kv="""
    
    BoxLayout:
        Button:
            size_hint:(.1,.1)
            text:'Light theme'
            on_press:
                app.Light()
                
        Button:
            size_hint:(.1,.1)
            text:'Dark theme'
            on_press:
                app.Dark()
    """
    class Main(MDApp):
        def build(self):
            
            return Builder.load_string(Kv)
                
                                            
        def Light(self):
            
            self.theme_cls.theme_style = "Light"
            return Builder.load_string(Kv)
            
        def Dark(self):
            
            self.theme_cls.theme_style = "Dark"
            return Builder.load_string(Kv)
            
    Main().run()
    
    
    

    默认情况下,kivymd 主题将设置为“Light”

    您可以在以下位置找到有关 Kivymd 主题的更多信息 https://kivymd.readthedocs.io/en/latest/themes/theming/index.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多