How to change label colour dynamically?

3,195 views
Skip to first unread message

David Aldrich

unread,
Jun 5, 2015, 9:54:46 AM6/5/15
to kivy-...@googlegroups.com
Hi

Sorry for asking lots of questions, but I am new to Kivy. I am using Kivy 1.9 with py3.4.

I want to change the background colour of a Label using Python code.

I know that I can do so in kv as follows:

    Label:
        text: 'Version'
        canvas.before: 
            Color: 
                rgb: .6, .6, .6 
            Rectangle: 
                pos: self.pos 
                size: self.size

but I am struggling to do this in Python. I have tried:

        glayout = GridLayout(cols=2,row_force_default=True, row_default_height=40)
        label1=Label(text='Version', font_size = 40)
        with label1.canvas:
            Color=(0.5,0.5,0.5)

        glayout.add_widget(label1)


but the background colour of the label remains black.

What am I doing wrong?

Best regards

David

Alexander Taylor

unread,
Jun 5, 2015, 10:06:54 AM6/5/15
to kivy-...@googlegroups.com
I would do this in kv anyway - it's easier there. However, here is a python version for reference:

from kivy.graphics import Color, Rectangle
with label1.canvas:
    Color(0.5, 0.5, 0.5)
    self.rect = Rectangle(pos=self.pos, size=self.size)

def update_rect(instance, value):
    instance.rect.pos = instance.pos
    instance.rect.size = instance.size
   
self.bind(pos=update_rect, size=update_rect)

David Aldrich

unread,
Jun 5, 2015, 10:09:51 AM6/5/15
to kivy-...@googlegroups.com
Hi 

Thanks for your answer. My reason for doing it in Python is that I won't know the number of labels I need in the grid until runtime. Unless there is a cool way of doing that in kv?

BTW thanks for your videos.

Best regards

David

Alexander Taylor

unread,
Jun 5, 2015, 10:11:37 AM6/5/15
to kivy-...@googlegroups.com
That doesn't mean you have to use python. For instance, you could make a
new class MyLabel and set the background color with a kv rule, then when
you make your unknown number they will all have that rule applied.

Actually, you would want to do this anyway even if just using kv, to
avoid writing the same graphics stuff many times.
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Kivy users support" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/kivy-users/fzFyI9jV-Lo/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> kivy-users+...@googlegroups.com
> <mailto:kivy-users+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


signature.asc

David Aldrich

unread,
Jun 8, 2015, 7:23:01 AM6/8/15
to kivy-...@googlegroups.com
Hi Alexander

Thanks for your reply. I'm sorry but I am really struggling to get this together.  Based on the ControllerApp example in the Kivy documentation I have put together a very simple app:

ControllerApp.py:

import kivy

from kivy.app import App
from kivy.properties import ObjectProperty

class Controller(FloatLayout):
    ''' Create a controller that revceives a custom widget from the kv lang file
    Add an action to be called from the kv lang flile
    '''
    bid = ObjectProperty()
    
class ControllerApp(App):
    def build(self):
        return Controller(info='Hello world')

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


ControllerApp.kv:

<Controller>:
    bid: my_box_layout
    
    BoxLayout:
        orientation: 'vertical'
        padding: 20
        id: my_box_layout
                    
        Label:
            id: my_custom_label
            text: 'Hello'
            canvas.before: 
                Color: 
                    rgb: .6, .3, .6 
                Rectangle: 
                    pos: self.pos 
                    size: self.size 

Now, I want to do two things:

  1. Define a MyLabel class in the .kv file and then add widgets of that class to the BoxLayout, all in the .kv file
  2. Next step, add the MyLabel widgets to the BoxLayout using code in .py.
Please will you show me how these steps can be done?

Best regards

David

David Aldrich

unread,
Jun 8, 2015, 9:16:35 AM6/8/15
to kivy-...@googlegroups.com
I've worked out the answer to Q1. I'll raise a new thread for Q2 and we can close this thread.

Reply all
Reply to author
Forward
0 new messages