我有以下 python 类:
import os
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from crawler.settings import ASSETS_DIR
class Map(GridLayout):
SIZE = 5
def __init__(self, **kwargs):
super(Map, self).__init__(**kwargs)
for _ in range(0, self.SIZE**2):
self.add_widget(Tile())
class Tile(Widget):
def __init__(self, **kwargs):
super(Tile, self).__init__(**kwargs)
self.add_widget(Image(source=os.path.join(ASSETS_DIR, 'images/chest.gif')))
以及下面的 kv 语言定义:
#:kivy 1.0.9
<Map>:
size: self.parent.size
<Tile>:
size: 20, 20
这只会渲染 1 个箱子(实际上循环运行正常,所以它们可能是堆叠的?): 如果我为一些开箱即用的小部件(如按钮)更改 Tile 类:
class Map(GridLayout):
SIZE = 5
def __init__(self, **kwargs):
super(Map, self).__init__(**kwargs)
for _ in range(0, self.SIZE**2):
self.add_widget(Button(text=str(_)))
我在我的 Tile 课程中缺少什么来完成这项工作?我认为这就是问题所在,但到目前为止我找不到