Kivy:如何合并单元格并与 MySQL 数据库交互

Kivy: How to merge cells and interact with MySQL database

这是我的 mykv.kv 文件的相关片段:

<RemoveScreen>:
    MyLayout:
        MyLogo:
            GridLayout:
                rows: 6
                cols: 2
                padding: 100,80,100,80
                Label:
                    font_size: "20sp"
                    bold: True
                    color: [1,1,0,1]
                    text: "Part number:"
                Label:
                    text: "Box 02"  
                Label:
                    font_size: "20sp"
                    bold: True
                    color: [1,1,0,1]
                    text: "Part description:"
                Label:
                    text: "Box 04"  
                Label:
                    font_size: "20sp"
                    bold: True
                    color: [1,1,0,1]
                    text: "Quatity on hand:"
                Label:
                    #font_size: "20sp"
                    text: "Box 06"
                Label:
                    font_size: "20sp"
                    bold: True
                    color: [1,1,0,1]
                    text: "Bin location:"   
                Label:
                    text: "Box 08"
                Label:
                    font_size: "20sp"
                    bold: True
                    color: [1,1,0,1]
                    text: "Direction:"  
                Label:
                    text: "Box 10"
                Label:
                    font_size: "20sp"
                    bold: True
                    color: [1,1,0,1]
                    text: "Scan time:"
                Label:
                    text: "Box 12"


        MyButtons:
            #buttons

上面的代码输出如下:

我想在顶部有一个合并单元格,它居中对齐,左列右对齐,右列左对齐。左列将从 MySQL 查询中获取字符串,并替换 "Box #" 字符串,如下所示:

问题: 您能否在我的代码中实现:

kivys GridLayout 中没有合并单元格的功能。 但是你可以解决这个问题,让它看起来像那样。
在 kivy 中很容易组合布局。您可以根据需要嵌套它们
因此,其中包含 2 个元素的垂直框布局可能是解决此问题的方法。

vertical BoxLayout
    Head Label  
    GridLayout

我会在这里给你举个例子。

python 文件只是一个最小的应用程序。

from kivy.app import App

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

Builder.load_file("kv.kv")


class RemoveScreen(BoxLayout):
    pass


class MyApp(App):
    def build(self):
        return RemoveScreen()


MyApp().run()

和 kv.kv 文件。为了使代码更简洁,我制作了自定义标签 类。这样你只需要在一个地方更改值。

<MyLabel1@Label>:
    font_size: "20sp"
    bold: True
    color: [1,1,0,1]
    halign: "right"
    text_size: root.width, None
    size: self.texture_size


<MyLabel2@Label>:
    halign: "left"
    text_size: root.width, None
    size: self.texture_size


<RemoveScreen>:
    orientation: "vertical"

    MyLabel1:
        text: "Headline"
        size_hint: (1,0.05)
        halign: "center"

    GridLayout:
        rows: 6
        cols: 2
        padding: [0, 0, 0, 25]
        spacing: [10,0]

        MyLabel1:
            text: "Part number:"
        MyLabel2:
            text: "Box 02"  

        MyLabel1:
            text: "Part description:"
        MyLabel2:
            text: "Box 04"  

        MyLabel1:
            text: "Quatity on hand:"
        MyLabel2:
            text: "Box 06"

        MyLabel1:
            text: "Bin location:"   
        MyLabel2:
            text: "Box 08"

        MyLabel1:
            text: "Direction:"  
        MyLabel2:
            text: "Box 10"

        MyLabel1:
            text: "Scan time:"
        MyLabel2:
            text: "Box 12"