0

In the following answer https://stackoverflow.com/a/56140831/9440453 how can i change the color of cell based on the values in it and also how can i fit the content within the cell or resize the cell based on the content size.

Details For color: If cell contains kyle the color should be red and if it contains raul it should be blue and so on

Which property we should edit to make this work.

Rasika
  • 13
  • 3

1 Answers1

0

Question 3 - text colour

how can i change the text color?

Solution 3 - Label color

Use color to change the text colour

Note

The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). To configure the button, the same properties (padding, font_size, etc) and sizing system are used as for the Label class.

Kivy Label » color

color

Text color, in the format (r, g, b, a).

color is a ListProperty and defaults to [1, 1, 1, 1].

Snippets

def apply_selection(self, rv, index, is_selected):
    ''' Respond to the selection of items in the view. '''

    self.selected = is_selected

    if rv.data[index]['text'].lower() == 'kyle':
        self.color = [0, 1, 0, 1]  # green colour text
        ...
    elif rv.data[index]['text'].lower() == 'raul':
        self.color = [0, 1, 1, 1]  # aqua colour text
        ...
    else:
        self.color = [1, 1, 1, 1]  # default white colour text
        ...

Question 1 - resize cell based on content

how can i fit the content within the cell or resize the cell based on the content size.

Solution 1 - Label texture_size

To set the size to the text content, bind size to texture_size to grow with the text.

Kivy Label » texture_size

texture_size

Texture size of the text. The size is determined by the font size and text. If text_size is [None, None], the texture will be the size required to fit the text, otherwise it’s clipped to fit text_size.

When text_size is [None, None], one can bind to texture_size and rescale it proportionally to fit the size of the label in order to make the text fit maximally in the label.

Warning

The texture_size is set after the texture property. If you listen for changes to texture, texture_size will not be up-to-date in your callback. Bind to texture_size instead.

Snippets

<Label>:
    size: self.texture_size

<TextInputPopup>:
    title: "Popup"

Question 2 - color cell according to data

how can i change the color of cell based on the values in it

Solution 2

The solution requires changes to the Python script.

Py file

  • Check for key e.g. kyle or raul in the method apply_selection(self, rv, index, is_selected) and set the rgba to red or blue respectively.
  • Change the colour by using Button's attributes, background_color and background_normal together.

Kivy Button » background_color

background_color

Background color, in the format (r, g, b, a).

This acts as a multiplier to the texture colour. The default texture is grey, so just setting the background color will give a darker result. To set a plain color, set the background_normal to ''.

The background_color is a ListProperty and defaults to [1, 1, 1, 1].

Kivy Button » background_normal

background_normal

Background image of the button used for the default graphical representation when the button is not pressed.

background_normal is a StringProperty and defaults to ‘atlas://data/images/defaulttheme/button’.

Snippets

def apply_selection(self, rv, index, is_selected):
    ''' Respond to the selection of items in the view. '''

    self.selected = is_selected

    if rv.data[index]['text'].lower() == 'kyle':
        self.background_color = [1, 0, 0, 1]    # red background colour
        self.background_normal = ''
    elif rv.data[index]['text'].lower() == 'raul':
        self.background_color = [0, 0, 1, 1]    # blue background colour
        self.background_normal = ''
    else:
        self.background_color = [1, 1, 1, 1]    # default colour
        self.background_normal = 'atlas://data/images/defaulttheme/button'    # default

Output

Result

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • if i apply the color for `mark` some other cells are also getting affected as in below screenshot https://imgur.com/a/7HoMiBV – Rasika May 15 '19 at 17:20
  • Please refer to updated post for resetting the colour. – ikolim May 15 '19 at 17:27
  • color works brilliantly well, Kivy is super awesome, never knew we could do such things.. i think i am not clear with content sizing question. I want the cell width to be exactly in size with the content width – Rasika May 15 '19 at 17:35
  • and also how can i change the text color? should i create a separate question for it? – Rasika May 15 '19 at 17:48
  • Please refer to updated post for solutions to text colour and resize based on content. – ikolim May 16 '19 at 14:47
  • For resizing, should i do anything other than giving `size: self.texture_size` in .kv file? – Rasika May 16 '19 at 16:51
  • As for resizing to content - nothing, just `size: self.texture_size`. – ikolim May 16 '19 at 23:18
  • for some reason , the resize not happening as expected – Rasika May 17 '19 at 14:04
  • Try replacing `size_hint: 1, None` with `size_hint: None, None` for both `GridLayout`s. The result is not very pretty. Due to this, I prefer to use `cols_minimum`. – ikolim May 17 '19 at 15:32