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

时间:2019-05-15 16:43:52

标签: kivy kivy-language

在下面的答案https://stackoverflow.com/a/56140831/9440453中,如何根据单元格中的值更改单元格的颜色,以及如何根据单元格中的内容调整单元格中的内容或根据内容大小调整单元格的大小。

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

我们应该编辑哪个属性以使其起作用。

1 个答案:

答案 0 :(得分:0)

问题3-文字颜色

  

如何更改文字颜色?

解决方案3-标签颜色

使用color更改文字颜色

注意

  

Button是一个Label,其相关联的动作是   按下按钮时触发(或在按下按钮后释放   点击/触摸)。要配置按钮,请使用相同的属性(填充,   Label类使用font_size等)和大小调整系统。

Kivy Label » color

color
     

文本颜色,格式为(r,g,b,a)。

     

颜色是ListProperty,默认为[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-根据内容调整单元格大小

  

我如何适应单元格中的内容或根据内容大小调整单元格的大小。

解决方案1-标签texture_size

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

Kivy Label » texture_size

texture_size
     

文本的纹理大小。大小由字体大小和   文本。如果text_size为[None,None],则纹理将为大小   需要适合文本,否则将其裁剪以适合文本   text_size

     

text_size为[None,None]时,可以绑定到texture_size和   按比例重新缩放以适合标签的大小,以便   使文本最大程度地适合标签。

     

警告

     

texture_size在texture属性之后设置。如果你听   对于texture的更改,texture_size不会是最新的   您的回叫。改为绑定到texture_size。

摘要

<Label>:
    size: self.texture_size

<TextInputPopup>:
    title: "Popup"

问题2-根据数据显示的彩色单元格

  

如何根据其中的值更改单元格的颜色

解决方案2

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

Py文件

  • 检查密钥,例如apply_selection(self, rv, index, is_selected)中的 kyle raul ,并将rgba分别设置为红色或蓝色。
  • 同时使用Button的属性background_colorbackground_normal更改颜色。

Kivy Button » background_color

background_color
     

背景色,格式为(r,g,b,a)。

     

这是纹理颜色的乘数。默认纹理   是灰色的,因此只需设置背景颜色即可使颜色更深   结果。要设置纯色,请将background_normal设置为”。

     

background_color是ListProperty,默认为[1、1、1,   1]。

Kivy Button » background_normal

background_normal
     

用于默认图形的按钮的背景图像   不按下按钮时的表示形式。

     

background_normal是StringProperty,默认为   “ 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

输出

Result