Search code examples
pythonkivykivy-language

Why does kivy show me a white background instead of the real rgba number?


In this program i set the background color of Label to 6, 61, 81, 1 like this :

        background_color: 6, 61, 81, 1
        canvas.before:
            Color:
                rgba:self.background_color

but in the out put kivy show me white background.

code

So I can not see the actual color that I set.

code :

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle 

Builder.load_string("""
    

<grid>
    GridLayout:
        cols:1
        size: root.width,root.height
        GridLayout:
            cols:2
            Label:
                size_hint: (1, None)
                height: 33
                text:'1'
                background_color: 6, 61, 81, 1
                canvas.before:
                    Color:
                        rgba:self.background_color
                    Rectangle:
                        size: self.size
                        pos: self.pos
                        
            TextInput:
                multiline:False
                height: 33
                size_hint: (5, None)
                background_color: 0,0,0,0
                foreground_color: 87, 30, 59, 0.8
                
            










""")

class grid(Widget):
    pass

class foo(App):

    def build(self):
        Window.clearcolor='#1618388'
        return grid()




    


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

However i set the rgba value to 1,0,0,1 it showed me correctly.

enter image description here

How can i solve this problem?


Solution

  • You are providing 8-bit integer RGB values, i.e. integers from 0 to 255 where 255 is fully saturated.

    Kivy's API uses floating point RGB values in the range 0-1.

    To convert from your units to those that Kivy expects, divide your RGB values by 255.