我的 Kivy 程序在左下角出现了一个随机的白色方块
My Kivy program has a random white square showing up in the bottom left corner
我正在尝试创建一个程序来输出随机的 10x10 黑白方块网格。除了左下角有一个不需要的白色方块覆盖了部分网格外,它大部分都有效。
我什至不知道是什么小部件导致的。我试过打印所有从根目录开始的 children 无济于事。
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
问题是你在不同的地方画了几次,恰好是在函数 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
#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
我正在尝试创建一个程序来输出随机的 10x10 黑白方块网格。除了左下角有一个不需要的白色方块覆盖了部分网格外,它大部分都有效。
我什至不知道是什么小部件导致的。我试过打印所有从根目录开始的 children 无济于事。
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
问题是你在不同的地方画了几次,恰好是在函数 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
#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