2

Is there a way to change the background_color of a Label without using canvas?

As i run the code, the background color is automaticlly black...

Here is my simple code:

from kivy.app import App

from kivy.uix.label import Label

from kivy.uix.boxlayout import BoxLayout

class MyWindow(App):

    def build(self):
        box = BoxLayout()
        label = Label(text='Hello World')
        box.add_widget(label)
        return box

window = MyWindow()

window.run()

Thank you

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
MaxKedem
  • 41
  • 1
  • 1
  • 9
  • Possible duplicate of [Change button or label text color in kivy](http://stackoverflow.com/questions/20437728/change-button-or-label-text-color-in-kivy) – White Shadow Nov 14 '16 at 15:50
  • @WhiteShadow Not a duplicate, OP asks if there's a different way as in the that question :P – Peter Badida Nov 14 '16 at 16:08

1 Answers1

0

Background color by itself, no. You'll still need to change it inside another widget or something similar. But if you use a picture of one color, then it is!

Label doesn't have a background by itself, that's why you can use its canvas to put it there, otherwise it's transparent. If it's transparent, that means it can show a content of another widget e.g. the one that's under it.

So put under it Image and you have basically the whole canvas + Rectangle with source thing, but separated into two widgets. If you want to change only background color, open e.g. mspaint, fill it with a single color and load with Python.

It might not work with BoxLayout correctly, because it handles position of its children automatically, but with FloatLayout that's not a problem anymore:

from kivy.app import App    
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout

class MyApp(App):

    def build(self):
        flt = FloatLayout()
        image = Image(size_hint=(None, None), size=(300, 300),
                      source=<path to image>)
        label = Label(size_hint=(None, None), size=(300, 300),
                      text='Hello World')
        flt.add_widget(image)
        flt.add_widget(label)
        return flt

MyApp().run()
Peter Badida
  • 11,310
  • 10
  • 44
  • 90