Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 将图片指定给对象列表_Python_Python 2.7_Tkinter_Python Imaging Library - Fatal编程技术网

Python 将图片指定给对象列表

Python 将图片指定给对象列表,python,python-2.7,tkinter,python-imaging-library,Python,Python 2.7,Tkinter,Python Imaging Library,我试图制作一个程序,生成一个对象列表,然后为每个对象指定一个“肖像” 现在我的代码如下: import random from Tkinter import * import ttk from characters import * from PIL import Image, ImageTk characters_creation() eyeslist = ["faces/eyes1.gif", "faces/eyes2.gif", "faces/eyes3.gif", "faces/e

我试图制作一个程序,生成一个对象列表,然后为每个对象指定一个“肖像”

现在我的代码如下:

import random
from Tkinter import *
import ttk
from characters import *
from PIL import Image, ImageTk

characters_creation()

eyeslist = ["faces/eyes1.gif", "faces/eyes2.gif", "faces/eyes3.gif", 
"faces/eyes4.gif", "faces/eyes5.gif", "faces/eyes6.gif", 
"faces/eyes7.gif", "faces/eyes8.gif", "faces/eyes9.gif", 
"faces/eyes10.gif", "faces/eyes11.gif"]

eblist = ["faces/eb1.gif", "faces/eb2.gif", "faces/eb3.gif", 
"faces/eb4.gif", "faces/eb5.gif"]

noselist = ["faces/nose1.gif", "faces/nose2.gif", "faces/nose3.gif", 
"faces/nose4.gif", "faces/nose5.gif", "faces/nose6.gif", 
"faces/nose7.gif"]

mouthlist = ["faces/mouth1.gif", "faces/mouth2.gif", "faces/mouth3.gif", 
"faces/mouth4.gif", "faces/mouth5.gif", "faces/mouth6.gif", 
"faces/mouth7.gif", "faces/mouth8.gif", "faces/mouth9.gif", 
"faces/mouth10.gif", "faces/mouth11.gif"]

fhlist = ["faces/fh1.gif", "faces/fh2.gif", "faces/fh3.gif", 
"faces/fh4.gif", "faces/fh5.gif", "faces/fh6.gif", 
"faces/fh7.gif", "faces/fh8.gif", "faces/fh9.gif", 
"faces/fh10.gif"]

mhlist = ["faces/mh1.gif", "faces/mh2.gif", "faces/mh3.gif", 
"faces/mh4.gif", "faces/mh5.gif", "faces/mh6.gif", 
"faces/mh7.gif", "faces/mh8.gif", "faces/mh9.gif", 
"faces/mh10.gif"]

root = Tk()

def RGBAImage(path):
    return Image.open(path).convert("RGBA")


for character in characters:

    face = RGBAImage("faces/face.gif")
    eb = RGBAImage(random.choice(eblist))
    eyes = RGBAImage(random.choice(eyeslist))
    nose = RGBAImage(random.choice(noselist))
    mouth = RGBAImage(random.choice(mouthlist))

    if character.sexe == "Female":
        hair = RGBAImage(random.choice(fhlist))
    else:
        hair = RGBAImage(random.choice(mhlist))


    face.paste(eyes, (0,0), eyes)
    face.paste(eb, (0,0), eb)
    face.paste(nose, (0,0), nose)
    face.paste(mouth, (0,0), mouth)
    face.paste(hair, (0,0), hair)

    facepic = ImageTk.PhotoImage(face)
    character.portrait.append(facepic)

for character in characters:

    cportrait = Label(image=character.portrait)
    cportrait.pack()


root.mainloop()
因此,每幅肖像都是由不同的元素(眼睛、鼻子等)构成的

运行此代码时,会收到以下错误消息:

Traceback (most recent call last):   File
"C:\Users\Patrick\Pictures\Kerrell\Kerrell Python\faces_generator.py",
line 67, in <module>
    cportrait = Label(image=character.portrait)   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2591, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2090, in __init__
    (widgetName, self._w) + extra + self._options(cnf)) TclError: image "[<PIL.ImageTk.PhotoImage instance at 0x027E6080>]" doesn't
exist
回溯(最近一次调用上次):文件
“C:\Users\Patrick\Pictures\Kerrell\Kerrell Python\faces\u generator.py”,
第67行,在
cportrait=Label(image=character.纵向)文件“C:\Python27\lib\lib tk\Tkinter.py”,第2591行,在__
Widget.\uuuu init\uuuu(self,master,'label',cnf,kw)文件“C:\Python27\lib\lib tk\Tkinter.py”,第2090行,在\uuuu init中__
(widgetName,self._w)+额外+self._选项(cnf))Tcl错误:图像“[]”不存在
存在
我不明白的是,如果我运行一个打印函数来检查我的人物肖像列表中有什么东西

character.portrait.append(facepic)
#...
cportrait = Label(image=character.portrait)
通过
追加
,我猜
肖像
是一个列表。您不能为标签的
图像
属性提供列表

更改
纵向
,使其不是列表:

character.portrait = facepic
或者从
纵向
列表中选择标签应具有的图像:

cportrait = Label(image=character.portrait[0]) #or whatever index
或显示所有这些内容:

for character in characters:
    for img in character.portrait:
        Label(image=img).pack()

谢谢完美地工作