繁体   English   中英

我如何在 python kivy 中创建一个表

[英]how can i create a table in python kivy

我正在编写一个 python 程序来计算两个文本输入中的几个值,但结果应该在一个表中......我将它创建为 label 但它不实用。 我怎样才能创建一个表! 非常感谢你

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()

用户应在第一个框中输入第一个值,然后输入空格,然后输入第二个值,依此类推。 然后他按 enter.. 然后可以在第二个框空间中继续第一个值,然后是第二个值。

MyMain.kv 文件

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

是的,桌子有点笨重。 虽然我不确定您是否需要所有这些框布局,但网格布局是否足够? 但除此之外,可以在带有格式化字符串和等宽字体的单个 Label 小部件上制作一个快速简单的表格,因为 Label 确实处理 \n 换行。 唯一的尴尬是 Label 同样不支持 \t 用于列之间的制表符间距,因此必须在 python 中提前呈现 \t 转义。这可以通过 expandtabs 字符串方法来完成。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM