label/image background color

6,395 views
Skip to first unread message

Pavel Kostelnik

unread,
Jun 18, 2012, 6:32:18 AM6/18/12
to kivy-...@googlegroups.com
Hi,

how do i set the background color for image/label/any other thing extending widget class? I tried canvas.before like this:

BoxLayout:
size_hint: 0.3, 1
orientation: 'vertical'
Image:
source: "settings.png"
Label:
canvas.before:
Color:
rgb: 0.6,0.6,0.6
text: "Settings"
id: settings_text
font_size: 20

But it just didn't work at all. The background remains black. It seemed to me from the docs that this would be the correct way of changing default background color. 

thnx

Mathieu Virbel

unread,
Jun 18, 2012, 6:43:11 AM6/18/12
to kivy-...@googlegroups.com
Hi,

The their is no background in image/label/any others things by default :)

If you want to add it, you were close to the result:

canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size

Regards,

Mathieu

On 18/06/2012 12:32, Pavel Kostelnik wrote:
> Hi,
>
> how do i set the background color for image/label/any other thing
> extending widget class? I tried canvas.before like this:
>
> �
> BoxLayout:
> size_hint: 0.3, 1
> orientation: 'vertical'
> Image:
> source: "settings.png"
> Label:
> canvas.before:
> Color:
> rgb: 0.6,0.6,0.6
> text: "Settings"
> id: settings_text
> font_size: 20
>
> �

Pavel Kostelnik

unread,
Jun 18, 2012, 6:45:44 AM6/18/12
to kivy-...@googlegroups.com
ah :) thanks :)
On Jun 18, 2012, at 12:43 PM, Mathieu Virbel wrote:

> Hi,
>
> The their is no background in image/label/any others things by default :)
>
> If you want to add it, you were close to the result:
>
> canvas.before:
> Color:
> rgb: .6, .6, .6
> Rectangle:
> pos: self.pos
> size: self.size
>
> Regards,
>
> Mathieu
>
> On 18/06/2012 12:32, Pavel Kostelnik wrote:
>> Hi,
>>
>> how do i set the background color for image/label/any other thing
>> extending widget class? I tried canvas.before like this:
>>
>> …
>> BoxLayout:
>> size_hint: 0.3, 1
>> orientation: 'vertical'
>> Image:
>> source: "settings.png"
>> Label:
>> canvas.before:
>> Color:
>> rgb: 0.6,0.6,0.6
>> text: "Settings"
>> id: settings_text
>> font_size: 20
>>
>> …

fpp gs

unread,
Apr 8, 2013, 2:29:12 PM4/8/13
to kivy-...@googlegroups.com
This seems to be a common riddle, and I'm having no success with this solution, probably doing it wrong...

Here is what I tried :

Builder.load_string('''
<CLabel@Label>:
    bgcolor: None
    canvas.before:
        Color:
            rgb: self.bgcolor
        Rectangle:
            size: self.size
            pos: self.pos
'''
)
class CLabel(Label):
    bgcolor
= ListProperty([0,0,0])



Then elsewhere :

lab = CLabel(text='Label with background ?', bgcolor=[.45,.46,.52,1])



... but the label always has the default style whatever I try.

What am I missing ?...

Akshay Arora

unread,
Apr 8, 2013, 3:54:29 PM4/8/13
to kivy-...@googlegroups.com
You don't need to redo the declarations  in kv and in python.


Builder.load_string('''
# define how clabel looks and behaves
<CLabel>:

    canvas.before:
        Color:
            rgb: self.bgcolor
        Rectangle:
            size: self.size
            pos: self.pos
'''
)

class CLabel(Label):
    bgcolor
= ListProperty([0,0,0])


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

fpp gs

unread,
Apr 8, 2013, 4:27:04 PM4/8/13
to kivy-...@googlegroups.com
Damn ! And I was just coming back to report exactly this... But thanks qua-non for the confirmation :-)

I would even say : "You need to NOT repeat the python declarations in Kv !"

Other kv examples (like templates etc.) have it the other way around, and I got them mixed up...

pdj...@gmail.com

unread,
May 27, 2013, 1:59:44 AM5/27/13
to kivy-...@googlegroups.com
Hi

I did as stated above (used Python instead of kv language). Not sure if I am doing it right though, I always get a small green background in the bottom left corner:

class myLabel(Label):
    def __init__(self,**kwargs):
        super(myLabel,self).__init__(**kwargs)
        with self.canvas.before:
                Color(0,1,0)
                Rectangle(pos = self.pos,size = self.size)

#Somewhere else in the code
        self.scroll_layout = GridLayout(cols=2,spacing=20,size_hint=(1,None))
        self.scroll_layout.bind(minimum_height=self.scroll_layout.setter('height'))

        for component in componentList:
            #some lines of code later            

            newButton = Button(text=str(component[0]),size_hint = (None,None),size=(200,40))
            newLabel = myLabel(text = str(component[1]),size_hint = (None,None),size=(100,40))

            #some lines of code later                        

            self.scroll_layout.add_widget(newButton)
            self.scroll_layout.add_widget(newLabel)

Akshay Arora

unread,
May 27, 2013, 4:58:11 AM5/27/13
to kivy-...@googlegroups.com
@pdjxyz, you should go through the programming guide the issue you are facing is explained in there.



Reply all
Reply to author
Forward
0 new messages