如何使用tkinter使用网格功能显示不同的图像? [英] how to display different images using grid function using tkinter?

查看:335
本文介绍了如何使用tkinter使用网格功能显示不同的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用grid()显示文件夹中的图像。但是当我尝试使用下面的代码时,我得到了迭代单个图像的输出。



我的代码:

def messageWindow():
赢=Toplevel()
path='C:\Users\HP\Desktop\dataset'
for r in range(7):
for c in range(10):
为glob.glob中的infile(os.path.join(path,'*.jpg')):
im=Image.open(infile)
resized=im.resize((100,100),Image.ANTIALIAS)
tkimage=ImageTk.PhotoImage(resized)
myvar=Label(win,image=tkimage)
myvar.image=tkimage
myvar.grid(row=r,column=c)
root=Tk()
button=Button(app,text='DataSet中的图像',command=messageWindow)
button.pack(padx=1,pady=1,anchor='ne')
button.place(x=850,y=60)
root.mainloop()

当我运行这段代码时,5分钟后会弹出一个子窗口,它将显示一个像这样的图像;





但是如何获取数据集中的所有图像?欢迎任何建议。
Tkanks for your support!

解决方案

正如我在评论中所说的,网格的行和列。以下是如何避免使用内置的divmod()函数根据要显示在每行中的COLUMNS的数量迭代计算每行的行数和列数基于当前值image_count

def messageWindow():
win=Toplevel()
path=r'C:\Users\HP\Desktop\dataset'
COLUMNS=10
image_count=0
对于glob.glob中的infile(os.path.join(path,'* .jpg')):
image_count+=1
r,c=divmod(image_count-1,COLUMNS)
im=Image.open(infile)
resized=im.resize((100,100),Image.ANTIALIAS)
tkimage=ImageTk.PhotoImage(resized)
myvar=Label(win,image=tkimage)
myvar.image=tkimage
myvar.grid(row=r,column=c)
win.mainloop()#不知道你是否也需要这个...


I want to display the images in the folder using grid(). But when I tried with the following code, I got the output with single image iterated.

My code:

def messageWindow():
    win = Toplevel()
    path = 'C:\Users\HP\Desktop\dataset'
    for r in range(7):
        for c in range(10):
            for infile in glob.glob(os.path.join(path,'*.jpg')):
              im = Image.open(infile)
              resized = im.resize((100, 100),Image.ANTIALIAS)
              tkimage = ImageTk.PhotoImage(resized)
              myvar=Label(win,image = tkimage)
              myvar.image = tkimage
              myvar.grid(row=r,column=c)   
root = Tk()
button = Button(app, text='Images in DataSet',command = messageWindow)
button.pack(padx = 1, pady = 1,anchor='ne')
button.place( x = 850, y = 60)
root.mainloop()  

When I run this code, after 5 minutes a child window will popup, and it will display a single image like this;

But how to get all the images in the dataset? Any suggestions are welcome. Tkanks for your support!

解决方案

As I said in a comment, you're putting the same image in every row and column of the grid. Here's how to avoid that using the built-in divmod() function to iteratively compute the row and column for each one, based on the number of COLUMNS you want to display in each row based on the current value of image_count:

def messageWindow():
    win = Toplevel()
    path = r'C:\Users\HP\Desktop\dataset'
    COLUMNS = 10
    image_count = 0
    for infile in glob.glob(os.path.join(path, '*.jpg')):
        image_count += 1
        r, c = divmod(image_count-1, COLUMNS)
        im = Image.open(infile)
        resized = im.resize((100, 100), Image.ANTIALIAS)
        tkimage = ImageTk.PhotoImage(resized)
        myvar = Label(win, image=tkimage)
        myvar.image = tkimage
        myvar.grid(row=r, column=c)
    win.mainloop()  # Not sure if you need this, too, or not...

这篇关于如何使用tkinter使用网格功能显示不同的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆