我的Kivy程序的左下角显示一个随机的白色正方形

时间:2019-01-16 02:17:00

标签: python python-3.x user-interface kivy kivy-language

White Box Error Kivy

我正在尝试创建一个程序,该程序输出随机的10x10黑白正方形网格。除左下角有一个多余的白色正方形覆盖了网格的一部分外,其余大部分都起作用。

我什至不知道是什么小部件会导致这种情况。我尝试从根目录开始打印所有孩子,但无济于事。

import random
import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.config import Config
from kivy.graphics import Color
from kivy.graphics import Rectangle


Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '400')

class Container(FloatLayout):
    pass

class ColorLabel(Label):
    def __init__(self, **kwargs):
        super(ColorLabel, self).__init__(**kwargs)

        with self.canvas:
            Color(1, 1, 1, 1)
            self.rect = Rectangle(size=self.size, pos=self.pos)

        self.bind(size=self._update_rect, pos=self._update_rect)

    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size

    def changeBG(self):
        with self.canvas.after:
            Color(0,0,0,1)
            self.rect = Rectangle(size=self.size, pos=self.pos)

class Main(App):
    def build(self):
        Builder.load_file("EveryImage.kv")
        the_grid = GridLayout(cols=10, spacing=1)

        i = 100
        while i > 0:
            i -= 1
            newLabel = ColorLabel()
            the_grid.add_widget(newLabel)
            x = random.randint(0,1)
            if x == 0:
                newLabel.changeBG()

        root = Container()
        root.add_widget(the_grid)           
        return root

# Keep everything below this last!      
if __name__ == '__main__':
    Main().run()

这是.kv文件:

#EveryImage.kv
Container:

#Container holds all the other layouts
<Container>:
    id: contain
    canvas.before:
        Color:
            rgba: 0,0,0.5,1 #blue, just for the grid
        Rectangle:
            pos: self.pos
            size: self.size

<ColorLabel>:
    canvas.before:
        Color:
            rgba: 1,1,1,1 #white
        Rectangle:
            pos: self.pos
            size: self.size

1 个答案:

答案 0 :(得分:2)

问题是您要在不同的位置上多次绘制,恰好是在changeBG函数中绘制,相反,您只需要在一个位置上绘制并将背景色设置为属性,以便在更改此值时可以重新绘制Label。

另一个错误是您正在创建一个不在.kv中使用的容器。

在while循环中,可以使用for循环来简化。

*。py

import random

import kivy
kivy.require("1.10.1")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.config import Config

Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '400')

class Container(FloatLayout):
    pass

class ColorLabel(Label):
    pass

class Main(App):
    def build(self):
        Builder.load_file("EveryImage.kv")
        the_grid = GridLayout(cols=10, spacing=1)
        for _ in range(100):
            newLabel = ColorLabel()
            the_grid.add_widget(newLabel)
            if random.choice([True, False]):
                newLabel.bg_color = [0,0,0,1]
        root = Container()
        root.add_widget(the_grid)           
        return root

# Keep everything below this last!      
if __name__ == '__main__':
    Main().run()

*。kv

#Container holds all the other layouts
<Container>:
    id: contain
    canvas.before:
        Color:
            rgba: 0,0,0.5,1 #blue, just for the grid
        Rectangle:
            pos: self.pos
            size: self.size

<ColorLabel>:
    bg_color: 1, 1, 1, 1
    canvas.before:
        Color:
            rgba: self.bg_color # white
        Rectangle:
            pos: self.pos
            size: self.size

enter image description here