Widget Position in GridLayout

I am trying to create a grid of widgets with each widget "cells" with a rectangle, which I can change later. When I run the code below with a line grid.add_widget(Button(text=str(i))), the buttons fill the window (as in the docs). However, when I use the widget Cellas in grid.add_widget(Cell()), all widgets are grouped in the lower left corner ( position=[0,0], size=[100,100]). I found this Kivy question : add a raw widget to the GridLayout (unlike Image, Button, etc.) and changed my class to Cellinherit from Layoutinstead Widget, but that didn't help. Any ideas on what I'm doing wrong? I would like to create a grid of colored rectangles.

Versions

[INFO   ] [Kivy        ] v1.9.1
[INFO   ] [Python      ] v2.7.6 (default, Jun 22 2015, 17:58:13) 

the code

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.layout import Layout
from kivy.graphics import Rectangle, Color

XSIZE=2
YSIZE=2

class Cell(Layout):
    def __init__(self, *args, **kwargs):
        super(Cell, self).__init__(*args, **kwargs)
        with self.canvas:
            Rectangle(size=self.size, pos=self.pos)
            Color((0.2, 0.2, 0.2, 1.0))

class GameApp(App):
    def build(self):
        grid = GridLayout(rows=YSIZE, cols=XSIZE, size=Window.size)
        for i in xrange(4):
            print 'i={}'.format(i)
#            grid.add_widget(Cell())
            grid.add_widget(Button(text=str(i)))
        return grid

if __name__ == '__main__':
    GameApp().run()
+4
source share
1

, . . ( gridlayouts). , InstructionGroup, .
, , .
MyGrid set_attributes.
, , , Clock.schedule_once. .
, , Clock.schedule_interval, .

, :

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import Rectangle, Color, InstructionGroup
from kivy.clock import Clock

from random import uniform

XSIZE=2
YSIZE=2

class Cell(Widget):
    def __init__(self, i, **kwargs):
        super(Cell, self).__init__(**kwargs)
        self.ig = InstructionGroup()
        self.rect = Rectangle()
        self.color = Color(0.2, 0.2, 0.2*i)
        self.ig.add(self.color)
        self.ig.add(self.rect)



class MyGrid(GridLayout):
    def __init__(self,**kwargs):
        super(MyGrid,self).__init__(**kwargs)
        self.rows=YSIZE
        self.cols=XSIZE
        for i in xrange(4):
            self.add_widget(Cell(i))
            self.canvas.add(self.children[0].ig)

        Clock.schedule_once(self.set_attributes)
        Clock.schedule_interval(self.change_color,1)

    def set_attributes(self,dt):
        for i in self.children:
            i.rect.pos = i.pos
            i.rect.size = i.size

    def change_color(self,dt):
        for i in self.children:
            i.color.rgb = (uniform(0.0,1.0),uniform(0.0,1.0),uniform(0.0,1.0))


class GameApp(App):
    def build(self):
        return MyGrid()


if __name__ == '__main__':
    GameApp().run()
+1

Source: https://habr.com/ru/post/1651236/


All Articles