Python Kivy中的AttributeError:GridLayout对象没有属性\u触发器\u布局
我正在尝试用python编写一个程序,它获取图像的一部分,并使用GridLayout在kivy中渲染它们 使用带有地图数据的XML文件,我从每个瓷砖的全局ID(或“GID”)中读取每个瓷砖,请参阅以了解其工作原理和程序使用过程的说明,将源瓷砖集图像转换为纹理,并使用.get_region()方法获取该纹理的一个区域。然后,我创建了一个图像小部件,将该部分纹理放入GridLayout。但我得到了这个错误: 回溯(最近一次呼叫最后一次): 文件“test_parse_xml.py”,第92行,在 地面=图层(根[1].attrib['name'],int(根[1].attrib['width']),int(根[1].attrib['height'])) 文件“test_parse_xml.py”,第85行,在init self.add_小部件(图像(当前_纹理))#返回错误。我做错了什么?Python Kivy中的AttributeError:GridLayout对象没有属性\u触发器\u布局,python,widget,textures,kivy,attributeerror,Python,Widget,Textures,Kivy,Attributeerror,我正在尝试用python编写一个程序,它获取图像的一部分,并使用GridLayout在kivy中渲染它们 使用带有地图数据的XML文件,我从每个瓷砖的全局ID(或“GID”)中读取每个瓷砖,请参阅以了解其工作原理和程序使用过程的说明,将源瓷砖集图像转换为纹理,并使用.get_region()方法获取该纹理的一个区域。然后,我创建了一个图像小部件,将该部分纹理放入GridLayout。但我得到了这个错误: 回溯(最近一次呼叫最后一次): 文件“test_parse_xml.py”,第92行,在 地
文件“/Applications/Kivy.app/Contents/Resources/Kivy/Kivy/uix/layout.py”,第85行,在add_小部件中 尺寸=自触发布局, AttributeError:“层”对象没有属性“\u触发器\u布局” 有没有办法解决这个问题?兴趣点是说
self.add_widget(Image(current_texture))
这是我的完整计划
import xml.etree.ElementTree as ET
from kivy.uix.gridlayout import GridLayout
from kivy.graphics.texture import Texture
from kivy.core.image import Image
from kivy.core.window import Window
tree = ET.parse('test_tileset.tmx')
root = tree.getroot()
#import general elements from <map> tag
mapWidth = int(root.attrib['width'])
mapHeight = int(root.attrib['height'])
tileWidth = int(root.attrib['tilewidth'])
tileHeight = int(root.attrib['tileheight'])
class TileSet(object):
"""Stores data about tilesets to be accessed by tiles from that tileset"""
def __init__(self, imagePath, imageWidth, imageHeight, #creating instance attributes of the class,
tilesetFirstGid, tilesetTileWidth, tilesetTileHeight): #change values with each instance
self.imagePath = imagePath
self.imageWidth = imageWidth
self.imageHeight = imageHeight
self.tilesetFirstGid = tilesetFirstGid
self.tilesetTileWidth = tilesetTileWidth
self.tilesetTileHeight = tilesetTileHeight
self.tilesetLastGid = (imageHeight//tilesetTileHeight)*(imageWidth//tilesetTileWidth)
#make a list of all the tilesets
tilesetList = []
#import data for each tileset from each <tileset> tag
Test = TileSet(root[0][0].attrib['source'], int(root[0][0].attrib['width']),
int(root[0][0].attrib['height']), int(root[0].attrib['firstgid']),
int(root[0].attrib['tilewidth']), int(root[0].attrib['tileheight'])
)
tilesetList.append(Test)
def get_tileset(gid):
"""takes an integer, the gid. Returns an instance of the tileset that has that gid"""
for i in tilesetList:
if gid <= i.tilesetLastGid:
return i
class Layer(GridLayout):
"""creates a grid of tiles based on information from a layer."""
def __init__(self, name, width, height, **kwargs):
self.name = str(name)
self.width = width
self.height = height
#set the number of columns of the gridlayout
self.cols = width
#get the layer for ease of iteration below
#using XPath to find all 'data' nodes that are children of nodes
#with the name of the instance of the class
self.layer = root.find(".//*[@name='"+self.name+"']/data")
prevgid = 1
current_texture = Image(Test.imagePath).texture
current_texture = current_texture.get_region(0, 0, 32, 32)
for gid in self.layer:
gid = int(gid.attrib['gid'])
if gid > 0:
if gid != prevgid:
ts = get_tileset(gid)
current_texture = Image(ts.imagePath).texture
#getting a region of the texture based off the Global ID (GID)
current_texture = current_texture.get_region((ts.imageWidth//ts.tilesetTileWidth-1)*ts.tilesetTileWidth,
(ts.imageHeight//ts.tilesetTileHeight-1)*ts.tilesetTileHeight,
ts.tilesetTileWidth, ts.tilesetTileHeight)
prevgid = gid
self.add_widget(Image(current_texture)) #returns an error. What am I doing wrong?
#Is there a better way to add the texture to the GridLayout?
else:
self.add_widget() #something will go here once I figure out my main problem
Ground = Layer(root[1].attrib['name'], int(root[1].attrib['width']), int(root[1].attrib['height']))
if __name__ == '__main__':
Ground().run()
将xml.etree.ElementTree作为ET导入
从kivy.uix.gridlayout导入gridlayout
从kivy.graphics.texture导入纹理
从kivy.core.image导入图像
从kivy.core.window导入窗口
tree=ET.parse('test_tileset.tmx'))
root=tree.getroot()
#从标记导入常规元素
mapWidth=int(root.attrib['width']))
mapHeight=int(root.attrib['height'])
tileWidth=int(root.attrib['tileWidth'])
tileHeight=int(root.attrib['tileHeight']))
类TileSet(对象):
“”“存储有关要由来自该瓷砖集的瓷砖访问的瓷砖集的数据”“”
def uuu init uuuu(self,imagePath,imageWidth,imageHeight,#创建类的实例属性,
tilesetFirstGid、TileSettleWidth、TileSettleWight):#随每个实例更改值
self.imagePath=imagePath
self.imageWidth=imageWidth
self.imageHeight=图像高度
self.tilesetFirstGid=tilesetFirstGid
self.tileSettleWidth=tileSettleWidth
self.tileSettleLight=tileSettleLight
self.tilesetLastGid=(imageHeight//TileSetTitleLight)*(imageWidth//TileSetTitleWidth)
#列出所有瓷砖组
tilesetList=[]
#从每个标记导入每个瓷砖集的数据
Test=TileSet(根[0][0]。attrib['source'],int(根[0][0]。attrib['width']),和,
int(根[0][0].attrib['height']),int(根[0].attrib['firstgid']),和,
int(根[0].attrib['tilewidth']),int(根[0].attrib['tileheight'])
)
tilesetList.append(测试)
def获取tileset(gid):
“”“获取一个整数,即gid。返回具有该gid的tileset实例”“”
对于tilesetList中的i:
如果gid为0:
如果gid!=普雷维吉德:
ts=获取tileset(gid)
当前纹理=图像(ts.imagePath).texture
#基于全局ID(GID)获取纹理区域
当前纹理=当前纹理。获取区域((ts.imageWidth//ts.TileSettleWidth-1)*ts.TileSettleWidth,
(ts.imageHeight//ts.TileSettleLight-1)*ts.TileSettleLight,
T.TileSettleWidth,T.TileSettleWidth)
prevgid=gid
self.add_小部件(图像(当前_纹理))#返回错误。我做错了什么?
#是否有更好的方法将纹理添加到GridLayout?
其他:
self.add_widget()#一旦我解决了主要问题,这里就会出现一些问题
地面=图层(根[1].attrib['name'],int(根[1].attrib['width']),int(根[1].attrib['height']))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
地面().run()
您需要在方法中调用超级构造函数
def __init__(self, name, width, height, **kwargs):
GridLayout.__init__(self, cols=2, row_force_default=True, row_default_height=40, spacing=[0, 1],**kwargs)
self.name = str(name)
self.width = width
self.height = height
谢谢这消除了错误,但我仍然无法让图像小部件正常工作:文件“kivy/\u event.pyx”,第438行,在kivy.\u event.EventDispatcher.bind(kivy/\u event.c:6026)KeyError:“size\u hint”)中,我使用self.canvas.add(矩形(纹理=当前纹理))并在屏幕左下角绘制了一个白色框。。。我需要弄清楚如何显示纹理区域。当我需要在屏幕上显示图像时,我通常使用kivy.uix.image.image
(但我也不制作游戏)