Change color MDLabel inside of a Popup in kivyMD

Question:

I’m trying to change the color of my MDLabel that I put inside of a popup in my KivyMD project, but I’m having a really bad time trying it. For some reason, that I don’t know color don’t change, I tried to change the theme_style to Dark and then the font color changed to white. But what i want is to keep the theme_style and change the label font color only.

Follow my code:

from kivymd.uix.label import MDLabel
from kivymd.uix.screen import Screen
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton
from kivy.uix.gridlayout import GridLayout
from kivymd.theming import ThemeManager
from kivy.uix.popup import Popup

from functools import partial
 
def layout_popup(title,mensagem,btn = None,args = None):
    layout = GridLayout(cols = 1, padding = 10)
    popupLabel = MDLabel(
        text = mensagem,
        color = (1,1,1,1),
        halign = "center")

    layout.add_widget(popupLabel)

    if(args != None):
        for key in args:
            print(args[key]["type"])
            if(args[key]["type"] == "button"):
                layout.add_widget(
                    Button(text = args[key]["text"],
                        on_press = args[key]["on_press"]
                    )
                )
    
    popup = Popup(title =title,
                    content = layout,
                    size_hint =(None, None),
                    size =(200, 200))
                    
    closeButton = MDRectangleFlatButton(text = "Fechar",
        pos_hint = {"center_x":0.5,"center_y":0.5},
        size_hint=  (1, None),
        on_press = popup.dismiss)

    layout.add_widget(closeButton)

    popup.open()  

class demo(MDApp):
    def build(self):
        layout_popup("mensagem","Erro")
        return None

demo().run()

If someone know how to solve this I would be really gratefull.

Asked By: Mateus Coelho

||

Answers:

According to the documentation:

To use a custom color for MDLabel, use a theme ‘Custom’. After that,
you can specify the desired color in the rgba format in the text_color
parameter

So try something like:

popupLabel = MDLabel(
    text=mensagem,
    theme_text_color="Custom",
    text_color=(1, 1, 1, 1),
    halign="center")
Answered By: John Anderson
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.