【发布时间】:2021-12-31 01:56:37
【问题描述】:
请仅在树视图中的最后一个插入项显示 tree.insert 中的图像,但我希望在任何地方都显示
这是我的代码
def openFichiersDossiers(h):
global img_file
global chemin
racine = tree1.selection()
b = (tree1.item(racine)['values'])[3]
chemin_absolu = Path(b)
if chemin_absolu.is_dir():
for x in tree1.get_children():
tree1.delete(x)
try:
for entry in os.listdir(chemin_absolu):
try:
path1 = Path(b + "\\" + entry)
if path1.is_dir():
try:
tree1.insert(parent='', index=0, text=entry,
values=[time.ctime(os.path.getmtime(path1)), "
Dossier",'', path1], image=img_dir)
except OSError as e:
showerror("Erreur", message=str(e))
else:
try:
img_file = function.icon(path1)
tree1.insert(parent='', index='end', text=entry,
values=[time.ctime(os.path.getmtime(path1)), "Fichier",
str(os.path.getsize(path1)) + ' Byte(s)',
path1],image=img_file)
except OSError as e:
showerror("Erreur", message=str(e))
except OSError as e:
showerror("Erreur", message=str(e))
except OSError as e:
showerror("Erreur", message=str(e))
chemin.append(chemin_acces.cget("text"))
这是我用来获取图像的函数
def icon(p):
if (os.path.splitext(p))[1] == ".docx":
img = PhotoImage(file='images/file_type_word_icon_130070.png')
return img
if (os.path.splitext(p))[1] == ".pdf":
img = PhotoImage(file='images/file-expand_Pdf_icon-icons.com_68956.png')
return img
if (os.path.splitext(p))[1] == ".mp4" or (os.path.splitext(p))[1] == ".mkv":
img = PhotoImage(file='images/videos_myvideos_play_2144.png')
return img
if (os.path.splitext(p))[1] == ".xlsx":
img = PhotoImage(file='images/1486565571-microsoft-office-excel_81549.png')
return img
if (os.path.splitext(p))[1] == ".png" or (os.path.splitext(p))[1] == ".jpeg" or (os.path.splitext(p))[1] == ".gif" \
or (os.path.splitext(p))[1] == ".ico" or (os.path.splitext(p))[1] == ".jpg":
img = PhotoImage(file='images/picture_photo_image_icon_131252.png')
return img
if (os.path.splitext(p))[1] == ".txt":
img = PhotoImage(file='images/txt_text_file_format_extension_icon_124600.png')
return img
if (os.path.splitext(p))[1] == ".mp3" or (os.path.splitext(p))[1] == ".m4a":
img = PhotoImage(file='images/apple_music_android_logo_icon_134021.png')
return img
if (os.path.splitext(p))[1] == ".iso":
img = PhotoImage(file='images/iso_20225.png')
return img
if (os.path.splitext(p))[1] == ".rar" or (os.path.splitext(p))[1] == ".zip" or (os.path.splitext(p))[1] == ".7z":
img = PhotoImage(file='images/winrar_14662.png')
return img
if (os.path.splitext(p))[1] == ".exe":
img = PhotoImage(file='images/exe_115515.png')
return img
if (os.path.splitext(p))[1] == ".ppt":
img = PhotoImage(file='images/microsoft_powerpoint_macos_bigsur_icon_189966.png')
return img
if (os.path.splitext(p))[1] == ".pub":
img = PhotoImage(file='images/Publisher_2013_23475.png')
return img
else:
img = PhotoImage(file='images/1492616984-7-docs-document-file-data-google-suits_83406.png')
return img
【问题讨论】:
-
首先编辑问题并使用特殊按钮格式化代码。
-
如果您有很多图像,请将它们保留在列表中 - 不要将所有图像分配给同一个变量
img_file。PhotoImage具有bug,当图像未分配给全局变量或类对象时,它会从内存中删除图像。如果你将第二张图片分配给同一个全局变量,那么bug从内存中删除前一张图片,你就看不到这张图片了。所以创建列表img_file = []并使用img_file.append( ...) -
我试过你的方法,但效果一样
-
请你写一段代码并发给我
-
外部函数:
all_images = [],内部函数:img_file = function.icon(path1),all_images.append(img_file)
标签: python image tkinter treeview