Kivy中的动态网格,每个网格元素包含多个小部件

时间:2017-12-31 03:24:53

标签: python user-interface widget kivy kivy-language

这是我在这里发表的第一篇文章,但我会尽量做到尽可能详细。 所以我的应用程序是在python中,并且涉及kivy中的网格,其中网格中的每个元素应该包含4个额外的小部件,并且可能包含第五个小部件。四个额外的小部件应该是边缘的十字形状和中间的第五个。

问题是,每当我添加一个子小部件时,它就会落在主窗口位置0,0的左下角

到目前为止一切顺利。现在我只是试图让另一个小部件中的一个小部件正确显示。

下面是我的尝试:

<GridCell@Label>
    BoxLayout:
        orientation:'horizontal'
        Button:
            text:'One'
            size:10,10
            size_hint:None,None

为我的应用构建一个.kv文件,我会在其中放置一个盒子布局,然后是一个按钮。

我还尝试了以下类配置:

class GridCell(Label):

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.root = FloatLayout()
    self.button = Button(text="test")
    self.button.x = self.root.x
    self.button.center_y = self.root.center_y
    self.root.add_widget(self.button)
    self.add_widget(self.root)

也没用。

我正在添加网格单元格,只需在网格上调用.add,并为for循环的每次迭代创建一个新创建的小部件。

所有儿童小部件显然都已创建,但它们都位于左下角!

这是gui现在的整个代码:

import kivy
import World
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Rectangle

kivy.require('1.10.0')



class GridCell(Label):

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.root = FloatLayout()
    self.button = Button(text="blargh")
    self.button.x = self.root.x
    self.button.center_y = self.root.center_y
    self.root.add_widget(self.button)
    self.add_widget(self.root)

def on_size(self, *args):
    self.canvas.before.clear()

    if self.id is "cliff":
        with self.canvas.before:
            Color(249 / 255, 6 / 255, 6 / 255, 0.3)
            Rectangle(pos=self.pos, size=self.size)
    if self.id is "goal":
        with self.canvas.before:
            Color(6 / 255, 6 / 255, 249 / 255, 0.3)
            Rectangle(pos=self.pos, size=self.size)
    if self.id is "start":
        with self.canvas.before:
            Color(11 / 255, 174 / 255, 6 / 255, 0.3)
            Rectangle(pos=self.pos, size=self.size)
    if self.id is "normal":
        with self.canvas.before:
            Color(119 / 255, 115 / 255, 115 / 255, 0.3)
            Rectangle(pos=self.pos, size=self.size)




class GameGridApp(App):

def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.grid = GridLayout(cols=8, rows=5)
    self.load_into()

def load_into(self):
    world = World.World(8, 5)
    world.build_gamegrid()

    for cell in world.gamegrid:
        name = str(cell.name)
        grid_cell = GridCell()
        grid_cell.text = name

        if cell.start:
            grid_cell.id = "start"
        if cell.goal:
            grid_cell.id = "goal"
        if cell.cliff:
            grid_cell.id = "cliff"
        if cell.field:
            grid_cell.id = "normal"

        self.grid.add_widget(grid_cell)

def build(self):
    return self.grid


customLabel = GameGridApp()
customLabel.run()

1 个答案:

答案 0 :(得分:1)

我可以提出一个想法,即创建一个'subgrids'对象和一个包含'subgrids'的'main grid'对象。这两个对象是GridLayout个对象。

以下是python2.7中的一个简单示例:

import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label

class SubGrids(GridLayout):
    def __init__(self):
        GridLayout.__init__(self, cols=3, rows=3);
        self.add_widget(Label(text='1st'));
        self.add_widget(Label(text=''));
        self.add_widget(Label(text='2nd'));
        self.add_widget(Label(text=''));
        self.add_widget(Label(text='3rd'));
        self.add_widget(Label(text=''));
        self.add_widget(Label(text='4th'));
        self.add_widget(Label(text=''));
        self.add_widget(Label(text='5th'));
class Grids(GridLayout):
    def __init__(self):
        GridLayout.__init__(self, cols=2, rows = 2);
        self.add_widget(SubGrids());
        self.add_widget(SubGrids());
        self.add_widget(SubGrids());
        self.add_widget(SubGrids());
class Example(App):
    def build(self):
        return Grids()

if __name__ == '__main__':
    x = Example();
    x.run();

希望这能给出一个想法。

enter image description here