python - 使用pillow在Tkinter界面上显示图片时不能理解的问题
迷茫
迷茫 2017-04-18 10:29:50
0
0
720

各位好。

我有一段代码,放在循环中和放在循环外出来的结果让我不能理解。代码如下:

#-*-coding:utf-8-*- import os,sys from PIL import Image,ImageTk import hashlib import Tkinter as tk os.chdir(os.path.split(os.path.abspath(sys.argv[0]))[0]) root=tk.Tk() im=Image.open(r'.\ERR\err.jpg') im=im.resize((376,250)) for x in range(1,4): #这一段代码放在循环之外和放在循环之内的区别################# photo=ImageTk.PhotoImage(im) im.close() ############################################################ locals()['x'+str(x)]=tk.LabelFrame(root).grid(row=x,column=0) tk.Label(locals()['x'+str(x)],image=photo).grid(row=x,column=0) root.mainloop()

其中井号之间的那一段代码如果放到循环外,显示就正常,如果放到循环里面,第一、第二次循环只显示空白的图片,非常不能理解。

以下是井号间代码在循环以外时的结果,也是我想要的。

以下是井号间代码在循环体内部时的结果,这个是我不能理解的。问题是我需要他在循环体内部(此处代码只是示例,实际代码中photo变量需要赋不同的值)。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

répondre à tous (0)
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!