KivyMD icon indicating copy to clipboard operation
KivyMD copied to clipboard

Cant set a button md_bg_color back to primary_color

Open dolfandringa opened this issue 4 years ago • 9 comments

Description of the Bug

When you create a MDRaisedButton with a different color than theme_cls.primary_color, you can't set it back to the primary_color. The issue is caused by the following code in kivymd.uix.button:

    def on_md_bg_color(self, instance, value):
        if value != [0.0, 0.0, 0.0, 0.0]:
            self._current_button_color = value

It prevents setting the color to the primary_color.

Code and Logs

from kivy.clock import Clock
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.screen import MDScreen

KV = '''
MyScreen
'''


class MyScreen(MDScreen):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.theme_cls = MDApp.get_running_app().theme_cls
        self.button = MDRaisedButton(text='CANCEL')
        self.button.md_bg_color = self.theme_cls.error_color
        self.add_widget(self.button)
        Clock.schedule_once(self.change_button, 10)

    def change_button(self, *_args, **_kwargs):
        self.button.text = "OK"
        self.button.md_bg_color = self.theme_cls.primary_color


class Test(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.screen = Builder.load_string(KV)

    def build(self):
        return self.screen

    def set_item(self, instance):
        # print(instance.text,"----------this is instance")
        gender = instance.text
        mm = self.root.ids.drop_item.set_item(gender)
        self.root.ids.drop_item.text = gender
        self.smenu.dismiss()


Test().run()

Versions

  • OS: any
  • Python: 3.7
  • Kivy: 1.11.1
  • KivyMD: master

dolfandringa avatar Sep 25 '20 02:09 dolfandringa

check the fork in the pull request #501 it's still a work in progress but have a lot of bugfixes for the button's lib and relatives to them, also some improvements

podraco avatar Sep 26 '20 01:09 podraco

i also had a problem related to button (especially button color) i had set error_color to a button but when the theme changes the error color changes to the primary_color

Code

from kivymd.app import MDApp
from kivymd.uix.picker import MDThemePicker
from kivy.lang import Builder

kv = """

Screen:
	MDRectangleFlatIconButton:
		text: 'DELETE ROOM'
		icon: 'delete'
		pos_hint: {'center_x': .5, 'center_y': .1}
		text_color: app.theme_cls.error_color
		on_release:
			print('on_release')

	MDIconButton:
		on_release:
			app.pick.open()
"""


class MainsApp(MDApp):
    def build(self):
        self.pick = MDThemePicker()
        return Builder.load_string(kv)


MainsApp().run()

kulothunganug avatar Sep 26 '20 09:09 kulothunganug

check this #501 .

podraco avatar Sep 27 '20 08:09 podraco

it's a major upgrade to kivyMD but it's not complete yet. some bugs shown up

podraco avatar Sep 27 '20 08:09 podraco

i also had a problem related to button (especially button color) i had set error_color to a button but when the theme changes the error color changes to the primary_color

Code

from kivymd.app import MDApp
from kivymd.uix.picker import MDThemePicker
from kivy.lang import Builder

kv = """

Screen:
	MDRectangleFlatIconButton:
		text: 'DELETE ROOM'
		icon: 'delete'
		pos_hint: {'center_x': .5, 'center_y': .1}
		text_color: app.theme_cls.error_color
		on_release:
			print('on_release')

	MDIconButton:
		on_release:
			app.pick.open()
"""


class MainsApp(MDApp):
    def build(self):
        self.pick = MDThemePicker()
        return Builder.load_string(kv)


MainsApp().run()

take in count that you're not assigning the error color, but a copy of the error color in theme_cls. for this in #501 i implemented theme_text_color, to set it to a variety of theme_CLS provided ones.

podraco avatar Oct 01 '20 18:10 podraco

My use-case is that I want to change color of the button on-the-fly. So the button starts as green for instance, and then turns red. I don't want to change the theme, I want to change the individual button from primary_color to error_color or back. So like this:

button = MDIconButton(text="OK")
button.md_bg_color = app.theme_cls.primary_color
# do something
button.text = "CANCEL"
button.md_bg_color = app.theme_cls.error_color

The above example works, but due to the check in the code, this does not:

button = MDIconButton(text="CANCEL")
button.md_bg_color = app.theme_cls.error_color
# do something
button.text = "OK"
button.md_bg_color = app.theme_cls.primary_color

dolfandringa avatar Oct 06 '20 04:10 dolfandringa

My PR solves exactly that problem

dolfandringa avatar Oct 06 '20 04:10 dolfandringa

@dolfandringa @podraco is currently making a big update for the button module, so let's wait until he finishes his work...

HeaTTheatR avatar Oct 06 '20 06:10 HeaTTheatR

ok cool. I'll just use my personal fork for now and keep that up-to-date with master.

dolfandringa avatar Oct 07 '20 06:10 dolfandringa

The issue is deprecated due to the new API of the KivyMD library 2.0.0 version

HeaTTheatR avatar Jan 10 '24 09:01 HeaTTheatR