how can i create a table in python kivy

Viewed 391

I'm writing a python program to compute few values in two textinput, but The Result should be in a table ... I created it as a label but it doesn't practical. how can i create a table ! Thank you very much

from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock



Window.clearcolor = (0.5, 0.5, 0.5, 1)


class WindowManager(ScreenManager):
    pass

class BestWindow(Screen):
    my_result = StringProperty("")
    inpt_one = ObjectProperty(None)
    result_layout = ObjectProperty(None)
    result_layout1 = ObjectProperty(None)
    result_layout2 = ObjectProperty(None)
    result_layout5 = ObjectProperty(None)
    test3 = ObjectProperty(None)
    def __init__(self, **kwargs):
        super(BestWindow, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)




    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if self.test3.focus and keyboard== 13:  # 32 - Cpace key presssed Ascii
            self.calculate()



    def calculate(self):
        self.result_layout.clear_widgets()
        self.result_layout1.clear_widgets()

        nums_one = self.inpt_one.text.strip().split(" ")
        nums_two = self.inpt_two.text.strip().split(" ")


        if(len(nums_one) < len(nums_two)):
            for _ in range(len(nums_two)-len(nums_one)):
                nums_one.append(0)
        elif(len(nums_two) < len(nums_one)):
            for _ in range(len(nums_one)-len(nums_two)):
                nums_two.append(0)
        result = [(int(x) + int(y)) for x,y in zip(nums_one, nums_two) ]

        aList = []
        for f in range(len(nums_one)):
            aList.append(f)
            self.ids.result_layout5.add_widget(Label(text='Num: {} '.format(f)))

        bList = []
        for i in range(len(nums_one)):
            self.ids.result_layout1.add_widget(Label(text='1.Gas: %{} '.format(nums_one[i])))
            bList.append(i)
        cList = []    
        for j in range(len(nums_two)):
            bList.append(j) 
            self.ids.result_layout2.add_widget(Label(text='2.Gas: %{} '.format(nums_two[j])))

        dList = []
        for res in result:
            res = int(res)
            dList.append(res)
            self.ids.result_layout.add_widget(Label(text='Sum: %{} '.format(res)))


        self.inpt_one.text = ''
        self.inpt_two.text = ''


kv = Builder.load_file("MyMain.kv")
class TestApp(App):
    def build(self):
        b1 = WindowManager()
        return b1



if __name__ == "__main__":
    TestApp().run()

the user should enter the first value in the first box and then space then the second value and so on. then he presses enter .. and can then continue first value in second box space then the second value.

MyMain.kv file

<CustButton@Button>:
    font_size: 40

<WindowManager>:
    BestWindow:


<BestWindow>:
    name: "erst"
    inpt_one: inpt_one
    inpt_two: inpt_two

    result_layout: result_layout
    result_layout1:result_layout1
    result_layout2:result_layout2
    result_layout5:result_layout5
    test3: test3
    GridLayout:
        spacing: 10
        cols:1
        Label:
            text: "das Gas 1"
            background_color: 1, 0, 0, 1

        TextInput:
            id:inpt_one
            focus : True
            text: ' '
            width: 100
            multiline: False
            on_text_validate: inpt_two.focus = True

        Label:
            text: "das Gas 2"
            background_color: 1, 0, 0, 1

        TextInput:
            id:inpt_two
            focus : True
            text: ' '
            width: 100
            multiline: False
            on_text_validate:
                test3.background_normal = ''
                test3.background_color = [0, 0, 1, 0.5]    # 50% translucent blue
                test3.focus = True
        Button:
            id : test3
            focus: False
            text: "Table!"
            on_press:
                root.calculate()
        BoxLayout:
            orientation: "horizontal"

        BoxLayout:
            id: result_layout5
            orientation: "horizontal"



        BoxLayout:
            orientation: "horizontal"

        BoxLayout:
            id: result_layout1
            orientation: "horizontal"



        BoxLayout:
            orientation: "horizontal"

        BoxLayout:

            id: result_layout2
            orientation: "horizontal"

        BoxLayout:
            orientation: "horizontal"

        BoxLayout:

            id: result_layout
            orientation: "horizontal"
1 Answers

Yes tables are a bit clunky. Although I'm not sure if you need all those box layouts, isn't the grid layout sufficient? But otherwise, a quick and easy table can be made on a single Label widget with formatted strings and a mono-spaced font, because Label does process \n for new line. The only awkwardness is that Label doesn't likewise support \t for tab spacing between the columns, so the \t escapes have to be rendered in advance in python. This can be done with expandtabs string method.

Label(text='l1c1\tl1c2\nll2c1\tll2c2'.expandtabs(8),font_name='UbuntuMono-R')
Related