Kivy中的动态网格,每个网格元素包含多个小部件 [英] Dynamic grid in Kivy with each grid element containing multiple widgets

查看:66
本文介绍了Kivy中的动态网格,每个网格元素包含多个小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一篇文章,但我会尽力详细介绍. 所以我的应用程序是在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)

也没有用.

我通过在for循环的每次迭代中使用一个新创建的小部件在网格上调用.add来添加网格单元格.

显然所有子项都已创建,但它们都落在左下角!

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

我可能会给出一个想法,即创建一个子网格"对象和一个包含子网格"的主网格"对象.这两个对象将是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();

希望这给出了一个主意.

This is my first post here, but I will try to be as detailled as I can. So my application is in python, and involves a grid in kivy, where each element in the grid is supposed to contain 4 additional widgets and possibility for a fifth. The four additional widgets are supposed to be in a cross shape at the edges and the fifth in the middle.

Problem is, whenever I add a sub widget it lands in the bottom left corner on position 0,0 of the main window

So far so good. Right now I am just trying to get even one widget inside another widget to display correctly.

Heres what I attempted:

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

Building a .kv file for my app, where I would put a box layout inside of it and then a button.

Also I tried the following class configuration:

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)

Also did not work.

I am adding the grid cells by just calling .add on the grid with a newly created widget for each iteration of a for loop.

All the child widgets are apparently created, but they all land in the bottom left corner!

This is the whole code of the gui right now:

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

解决方案

I may give an idea , that create a 'subgrids' object and a 'main grid' object that contain the 'subgrids'. These two objects would be GridLayout objects.

Here is a simple example in 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();

Hope this gives an idea.

这篇关于Kivy中的动态网格,每个网格元素包含多个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆