简体   繁体   中英

Kivy button on_press background colour is not changing

Kivy Button component on_press colors are not working in Kivy 2.0.0.

I check this Question and followed the answers, however, those answers didn't resolve the problem that I am facing.

My UI kv file button component code

Button:
    text: Settings
    background_color: (.3, 2.15, .96, 1)
    on_press: self.background_color = (1.0, 0.0, 0.0, 1.0)
    on_press: root.on_settings_click()
    on_press: self.background_color = (.3, 2.15, .96, 1)

Python code for button response

class SettingsMenu(Panel):
    def on_general_settings_click(self):
        self._parent.show_new_panel(GeneralSettings(self._parent))

EDIT:

I used on_release also doesn't make any change on the UI button component. Is there anything else I'm missing here?

    on_press: self.background_color = (1.0, 0.0, 0.0, 1.0)
    on_press: root.on_general_settings_click()
    on_release: self.background_color = (.3, 2.15, .96, 1)

Changing the background_color with on_press works in Kivy 2.0.0. But it may not show up in your GUI if the changes happen too quickly. Changing the background_color twice in the on_press event response will likely just show its last setting, so consider also using on_release . The other thing to keep in mind is that background_color is just a multiplier on the background_normal and background_down images. If you want a pure color for the normal or down states, then you must set background_normal and/or background_down to '' . Here is an example using these principles:

Button:
    text: 'Settings'
    background_normal: ''
    background_down: ''
    background_color: (1, 0, 0, 1)
    on_press: self.background_color = (0, 1, 0, 1.0)
    on_press: root.on_settings_click()
    on_release: self.background_color = (0, 0, 1, 1)

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