Search code examples
pythonpython-2.7kivykivy-language

python/kivy : Make a Label Bold in kivy


When i pass blank value in array

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

then it show 0 instead of blank value.
1. Can someone tell me how to set blank by deafault instead of 0 ?
2. How to make key(Item1) bold?

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'

Solution

  • When I proposed this code in a previous question I assumed that the second fields were always going to be integers and not string, so create the value: 0 property which, as seen, accepts only numerical values since it is mapped as NumericProperty. The solution is to convert it to StringProperty by changing to value:"" besides changing the line of convert_data so that it does the conversion of integers to string.

    In the second case to be able to use bold in the text we must set markup to True in Label and use [b] [/b] before and after the text that we want to modify.

    All of the above implies the following changes:

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