python / kivy:用kivy制作一个Bold标签

时间:2018-03-08 07:44:26

标签: python python-2.7 kivy kivy-language

当我在数组中传递空白值

arr = ({'Item1': ''},{'Item2': 1000})

然后显示0而不是空白值 有人能告诉我如何用空洞而不是0来设置空白吗? 2.如何使密钥(Item1)变粗?

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen

def convert_data(data):
    l = []
    for item in data:
        for key, value in item.items():
            l.append({'text': key, 'value': value})
    return l

class Invoice(Screen):
    def abc(self):
        #fetching from database
        arr = ({'Item1': ''},{'Item2': 1000})

        # convert to [{'text': 'Item1', 'value': 5000}, {'text': 'Item2', 'value': 1000}]
        self.rv.data = convert_data(arr)

class MyApp(App):
    def build(self):
        return Builder.load_file('test.kv')

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

test.kv

<Button@Button>:
    font_size: 15
    size_hint_y:None
    height: 30

<Label@Label>:
    font_size: 15
    size_hint_y:None
    height: 30

<Item@GridLayout>:
    cols: 2
    text: ""
    value: 0
    padding : 5, 0
    spacing: 10, 0
    Label:
        size_hint_x: .35
        text: root.text
        halign: 'left'
        valign: 'middle'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size

    Label:
        size_hint_x: .15
        text: str(root.value)
        halign: 'right'
        valign: 'middle'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size

Invoice:
    rv: rv
    BoxLayout:
        orientation: "vertical"
        padding : 15, 15

        BoxLayout:
            orientation: "vertical"
            padding : 5, 5
            size_hint: .6, None
            pos_hint: {'x': .18,}


            BoxLayout:
                orientation: "horizontal"
                padding : 5, 5
                spacing: 10, 10
                size: 800, 40
                size_hint: 1, None

                Button:
                    text: "Show"
                    size_hint_x: .05
                    spacing_x: 30
                    on_press:root.abc()

        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, 1

            BoxLayout:
                orientation: "vertical"
                size_hint: .5, 1
                padding : 0, 15
                spacing: 10, 10
                size: 500, 30

                Button:
                    text: "Invoice"
                    text_size: self.size
                    halign: 'center'
                    valign: 'middle'

                BoxLayout:
                    RecycleView:
                        id: rv
                        viewclass: 'Item'
                        RecycleBoxLayout:
                            default_size: None, dp(30)
                            default_size_hint: 1, None
                            size_hint_y: None
                            height: self.minimum_height
                            orientation: 'vertical'

1 个答案:

答案 0 :(得分:2)

当我在上一个问题中提出这个代码时,我假设第二个字段总是整数而不是字符串,所以创建value: 0属性,如图所示,它只接受数值,因为它被映射为NumericProperty。解决方案是将StringProperty更改为value:"",然后更改convert_data的行,以便将整数转换为字符串。

在第二种情况下,为了能够在文本中使用粗体,我们必须在标签中将标记设置为True,并在我们要修改的文本之前和之后使用[b] [/b]

以上所有意味着以下变化:

<强> *。PY

[...]

def convert_data(data):
    l = []
    for item in data:
        for key, value in item.items():
            l.append({'text': key, 'value': str(value)}) # change this line
    return l

[...]

<强> *。KV

[...]

<Item@GridLayout>:
    cols: 2
    text: ""
    value: ""
    padding : 5, 0
    spacing: 10, 0
    Label:
        size_hint_x: .35
        text: "[b]{}[/b]".format(root.text)
        markup: True
        halign: 'left'
        valign: 'middle'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size

    Label:
        size_hint_x: .15
        text: root.value
        halign: 'right'
        valign: 'middle'
        canvas.before:
            Color:
                rgb: .6, .6, .6
            Rectangle:
                pos: self.pos
                size: self.size
[...]