python如何实现多个图片显示在屏幕上?

xiamin 2016-02-10 06:41:23
python如何实现多个图片显示在屏幕上?
from tkinter import *

root = Tk()
# 创建一个Canvas,设置其背景色为白色 
cv = Canvas(root, bg = 'white', width = 500, height = 650)
rt = cv.create_rectangle(10,10,110,110,outline='red',stipple='gray12',fill='green')
for i in range(0,3):
print(i)
img=PhotoImage(file='D:\\python\\bmp\\'+str(i)+'.gif')
cv.create_image((20*i,200*i),image=img)
cv.pack()
root.mainloop()
只能显示2.gif ,前面0.gif,1.gif都不显示出来,为什么?
...全文
5011 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 panghuhu250 的回复:
引用
image= The image object. This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage). The application must keep a reference to the image object.

from tkinter import *
 
root = Tk()
cv = Canvas(root, bg = 'white', width = 500, height = 650) 
rt = cv.create_rectangle(10,10,110,110,outline='red',stipple='gray12',fill='green')
imgs= [PhotoImage(file='/tmp/'+str(i)+'.gif') for i in range(3)]
for img in imgs:
   cv.create_image((20*i,200*i),image=img) 
cv.pack()
root.mainloop() 
另外, 也许你知道:
'D:\\python\\bmp\\'
可以用"raw string":
r'D:\python\bmp\'
即使是在windows下,也可以用
'D:/python/bmp/'
cv.create_image((20*i,200*i),image=img) NameError: name 'i' is not defined
  • 打赏
  • 举报
回复
怎么提示错误呢 cv.create_image((20*i,200*i),image=img) NameError: name 'i' is not defined
xiamin 2016-02-11
  • 打赏
  • 举报
回复
太感谢了!!!
panghuhu250 2016-02-10
  • 打赏
  • 举报
回复
引用
image= The image object. This should be a PhotoImage or BitmapImage, or a compatible object (such as the PIL PhotoImage). The application must keep a reference to the image object.

from tkinter import *
 
root = Tk()
cv = Canvas(root, bg = 'white', width = 500, height = 650) 
rt = cv.create_rectangle(10,10,110,110,outline='red',stipple='gray12',fill='green')
imgs= [PhotoImage(file='/tmp/'+str(i)+'.gif') for i in range(3)]
for img in imgs:
   cv.create_image((20*i,200*i),image=img) 
cv.pack()
root.mainloop() 
另外, 也许你知道:
'D:\\python\\bmp\\'
可以用"raw string":
r'D:\python\bmp\'
即使是在windows下,也可以用
'D:/python/bmp/'

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧