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

时间:2016-08-05 01:22:00

标签: kivy kivy-language

以下是 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

上面的代码输出:

enter image description here

我希望在顶部有一个合并的单元格,其中心是对齐的,左列是右对齐的,右列是左对齐的。左列将从MySQL查询中获取字符串,并替换&#34; Box#&#34;字符串,看起来像:

enter image description here

问题: 你可以在我的代码中实现:

  • 将两个单元格的第一行合并为一个
  • 右对齐左栏
  • 左对齐右列(根据上面的布局)

1 个答案:

答案 0 :(得分:2)

在kivys GridLayout中,没有加入单元格的功能。 但你可以围绕这个做一个工作,使它看起来像那样 在kivy中,很容易组合布局。你可以随心所欲地筑巢 因此,一个垂直的boxlayout,其中包含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文件。为了使代码更清晰,我制作了自定义Label类。这样你只需要在一个地方改变价值。

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