Home > Enterprise >  How to change background color in kivy?
How to change background color in kivy?

Time:02-01

I have just started learning kivy. Please bear with me in case of my absurdity and stupidity.

Here just copy pasted the code from official site.

import kivy
from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
   def build(self):
      return Label(text='Hello world',)

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

And the output is in the link.

What I essentially wanted to know that how I can change the background (currently black) to some different color.

I have read some parts of documents ,Found documentation for changing color of widget but screen (probably not the accurate word).

I would really appreciate the advices and suggestions.

Thanks in advance.

CodePudding user response:

You can use the canvas of the Label to paint a background color like this:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label

class MyLabel(Label):
    pass

Builder.load_string('''
<MyLabel>:
    canvas.before:
        Color:
            rgba: 1,0,0,1
        Rectangle:
            pos: self.pos
            size: self.size
''')

class MyApp(App):
   def build(self):
      return MyLabel(text='Hello world',)

if __name__ == '__main__':
    MyApp().run()
  • Related