更改单元格的颜色并调整内容的大小

Changing the color of cell and sizing the content

在下面的回答中 我如何根据单元格中的值更改单元格的颜色,以及如何根据内容大小调整单元格中的内容或调整单元格的大小。

颜色详情: 如果单元格包含 kyle,则颜色应为 red,如果包含 raul,则颜色应为 blue,依此类推

我们应该编辑 属性 才能使这项工作正常进行。

问题 3 - 文字颜色

how can i change the text color?

解决方案 3 - 标签颜色

使用color改变文字颜色

备注

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].

片段

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
        ...

问题 1 - 根据内容调整单元格大小

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

解决方案 1 - 标签 texture_size

要将大小设置为文本内容,请将 size 绑定到 texture_size 以随文本增长。

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.

片段

<Label>:
    size: self.texture_size

<TextInputPopup>:
    title: "Popup"

问题 2 - 根据数据为单元格着色

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

解决方案 2

该解决方案需要更改 Python 脚本。

Py文件

  • 检查密钥,例如kyleraul在方法apply_selection(self, rv, index, is_selected)中分别设置rgba为红色或蓝色。
  • 使用 Button 的属性更改颜色,background_colorbackground_normal 一起使用。

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’.

片段

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

输出