python - 使用 for 循环、tkinter 显示多个图像
问题描述
我想在窗口的目录中显示多个图像
但是通过这段代码,一次只显示一张图片
这是我的代码:
base = Tk()
base.geometry('1000x1000')
folder_selected = filedialog.askdirectory()
txtfiles = []
for file in glob.glob(str(folder_selected) + "/*.jpg"):
txtfiles.append(file)
a = 10
def createIMG():
global a, img
for location in txtfiles:
n = Label(base, text='asd')
n.place(x=a, y=10)
img = Image.open(location)
img = img.resize((250, 250))
img = ImageTk.PhotoImage(img)
panel = Label(base, image=img)
panel.place(x=a,y=10)
a = a + 200
createIMG()
# threading.Thread(target=createIMG).start()
mainloop()
解决方案
以为这很容易,但花了很多时间才弄清楚。您需要一个容器来存放所有图像.open()
#! /usr/bin/python3
import os
import glob
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import filedialog
from PIL import ImageTk, Image
## ImportError: cannot import name 'ImageTk' from 'PIL'
## pip3 install --upgrade --force-reinstall pillow
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
title = 'PyThumbs'
root = tk .Tk() ; root .title( title )
root .bind( '<Escape>', lambda e: root .destroy() )
root .attributes( '-zoomed', True )
root .update_idletasks()
rootw, rooth = root .winfo_width(), root .winfo_height()
home = os .environ['HOME']
folder_selected = filedialog .askdirectory( initialdir = home, title = 'Select Folder' )
imagenames = []
for filename in glob .glob( str( folder_selected ) +'/*.jpg' ):
imagenames .append( filename )
cols, rows = 4, 3
cellw, cellh = rootw //cols, rooth //rows
texth = cellh //12
photow, photoh = cellw, cellh -texth
images = []
for col in range( cols ):
images .append( [] )
for row in range( rows ):
images[ col ] .append( [] )
def createIMG( c, r, i ):
fullpath = imagenames[ i ]
directory, filename = os .path .split( fullpath )
name, extension = os .path .splitext( filename )
print( c, r, i, fullpath )
img = Image .open( fullpath )
ratio = min( photow /img .width, photoh/ img .height )
thumb = img .resize( ( int( img .width *ratio ), int( img .height *ratio ) ) )
images[ c ][ r ] = ImageTk .PhotoImage( image = thumb )
cell = ttk .Button( root, name = f'{c}cell{r}', image = images[ c ][ r ] )
cell .place( x = c *cellw, y = r *cellh, width = cellw, height = photoh )
text = tk .Label( root, name = f'{c}text{r}', text = name[:20] )
text .place( x = c *cellw, y = (r +1) *cellh -texth, width = cellw, height = texth )
index = 0
for col in range( cols ):
for row in range( rows ):
try:
createIMG( col, row, index )
## t = createIMG( col, row, index )
## threading .Thread( target = t ) .start()
except: pass
index += 1
root .mainloop()
推荐阅读
- python - TypeError:'float'对象在 Python 中不可迭代......昨天还在工作
- c# - 使用 SelectToken JObject 递归列出元素 JSON
- xcode - 由于升级到 Xcode 12,无法将自定义 plist 文件添加到产品目标
- php - 在 nginx 上安装 roundcube 会导致 HTTP ERROR 500
- java - springboot logback:java.lang.ClassNotFoundException:org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout
- javascript - 使用新插入的内容滚动到 div 的底部
- linux - 创建 Lightsail 实例并安装 CodeDeploy 代理
- vue.js - 更改 element-ui Select 中的图标
- python - 为什么我的石头剪刀布游戏不启动while循环?
- jenkins - 如何在詹金斯中获得并行构建作业结果