简体   繁体   中英

Change Switch colour in python kivy?

Is it possible to change the colour of a kivy Switch widget?

I know that for a Button widget you set eg background_color: 1, 0, 0, 1 .

So I tried setting background_color to change the colour of my Switch . Strangely, it doesn't change the colour of my Switch but there is no error message either .

Thanks!

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

kv = '''
<RedSwitch>:
size_hint: None, None
size: 100, 100
    
Switch:
    id: new_switch  
    background_color: 1, 0, 0, 1
    pos: 50, 50
    size_hint: None, None
    size: 100, 50
    active: False   
    on_active: switch_callback() 
    
'''

class RedSwitch(FloatLayout):
        
    def switch_callback(self):
        pass       
        
class SwitchApp(App):
    def build(self):
        Builder.load_string(kv)
        return RedSwitch()

if __name__ == '__main__':
    SwitchApp().run()

Based on this: kivy: How to change Switch default from ON/OFF to OPEN/CLOSE or to YES/NO?

kv = '''
<RedSwitch>:
    size_hint: None, None
    size: 100, 100

    Switch:
        id: new_switch  
        active: False   
        on_active: root.switch_callback()

        canvas:
            Color:
                rgb: 1, 0, 0, 1
            Rectangle:
                size: sp(41.5), sp(20)
                pos: self.center_x - sp(40.0), self.center_y - sp(10)
            Color:
                rgb: 0, 0, 1, 1
            Rectangle:
                size: sp(41.0), sp(20)
                pos: self.center_x, self.center_y - sp(10)
        Label:
            color: 1, 1, 1, 1
            text: 'ON'
            bold: True
            font_size: 13
            pos: self.parent.center_x - sp(70), self.parent.center_y - sp(50)                
        Label:
            color: 0, 1, 0, 1
            text: 'OFF'
            bold: True
            font_size: 13
            pos: self.parent.center_x - sp(30), self.parent.center_y - sp(50)
'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM