Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的Kivy程序在左下角有一个随机的白色方块_Python_Python 3.x_User Interface_Kivy_Kivy Language - Fatal编程技术网

Python 我的Kivy程序在左下角有一个随机的白色方块

Python 我的Kivy程序在左下角有一个随机的白色方块,python,python-3.x,user-interface,kivy,kivy-language,Python,Python 3.x,User Interface,Kivy,Kivy Language,我试图创建一个程序,输出随机10x10的黑白方块网格。除了左下角有一个不需要的白色正方形覆盖了网格的一部分外,它基本上都能正常工作 我甚至不知道是什么小部件导致了这种情况。我试着从root开始打印所有的孩子,但没有结果 import random import kivy kivy.require("1.10.1") from kivy.app import App from kivy.lang import Builder from kivy.uix.floatlayout import Flo

我试图创建一个程序,输出随机10x10的黑白方块网格。除了左下角有一个不需要的白色正方形覆盖了网格的一部分外,它基本上都能正常工作

我甚至不知道是什么小部件导致了这种情况。我试着从root开始打印所有的孩子,但没有结果

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
#EveryImage.kv
容器:
#容器包含所有其他布局
:
id:包含
在以下情况之前:
颜色:
rgba:0,0,0.5,1#蓝色,仅用于网格
矩形:
pos:self.pos
大小:self.size
:
在以下情况之前:
颜色:
rgba:1,1,1,1#白色
矩形:
pos:self.pos
大小:self.size

问题在于,您在不同的位置绘制了多次,确切地说是在函数changeBG中,而您只需在一个位置绘制,并将背景色设置为属性,因此当此值更改时,标签将重新绘制

另一个错误是您正在创建一个不在.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

#容器包含所有其他布局
:
id:包含
在以下情况之前:
颜色:
rgba:0,0,0.5,1#蓝色,仅用于网格
矩形:
pos:self.pos
大小:self.size
:
背景颜色:1,1,1,1
在以下情况之前:
颜色:
rgba:self.bg#u color#白色
矩形:
pos:self.pos
大小:self.size

我在kv文件的第15行中不断遇到错误:“TypeError:“NoneType”对象不可编辑”。@PlugFire我已经更新了代码,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