如何使用 python Tkinter 迭代图像? [英] How to use python Tkinter to iterate images?

查看:31
本文介绍了如何使用 python Tkinter 迭代图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 python Tkinter 来迭代图像?

How to use python Tkinter to iterate images?

import tkinter as tk
from PIL import ImageTk, Image

win = tk.Tk()
win.geometry('800x500')  # set window size
win.resizable(0, 0)  # fix window

images = ['01.jpg', '02.jpg', '03.jpg']

def next_img():
   # show next image

for img in images:
    img = Image.open(img)
    img = ImageTk.PhotoImage(img)
    panel = tk.Label(win, image=img)
    panel.pack()
    btn = tk.Button(text='Next image', command=next_img)
    btn.pack()
win.mainloop()

但是我的面板没有显示任何图像.我希望面板等待我,然后单击按钮以显示下一张图像.如何解决.

But my panel doesn't show any images. I hope the panel waits me and I click the button to show next images. How to solve it.

推荐答案

你的代码不显示任何图像的原因有点复杂.有两个因素共同作用:

The reason why your code doesn't display any images is a bit complicated. There are two factors working together:

  1. 因为您要循环创建标签(用于显示图像的标签),所以最终会创建 3 个标签和 3 个按钮 - 每个图像一个.所有这些都排列在彼此下方,因此根据图像的大小,其中一些可能位于窗口底部边缘的下方.如果您使用小图片,您会在窗口中看到三个下一张图片"按钮.
  2. 如果您未在 Python 代码中保留对 Tkinter 图像的引用,则会对其进行垃圾收集.由于您的循环会在每次迭代中覆盖 img 变量的值,因此除最后一张之外的所有图像都被垃圾收集并且不会显示.
  1. Because you're creating your labels (the ones that you use to display the images) in a loop, you end up creating 3 labels and 3 buttons - one for each image. All of these are arranged below each other, so depending on the size of your images, some of them might be below the window's bottom edge. If you use small images, you'll see three "Next image" buttons in your window.
  2. Tkinter images are garbage collected if you don't keep a reference to them in your python code. Since your loop overwrites the value of the img variable in each iteration, all images except the last one are garbage collected and aren't displayed.

<小时>

要修复您的代码,首先要删除该循环.您不需要 3 个标签和 3 个按钮,也不需要在程序启动时立即加载所有 3 个图像.


To fix your code, first start by removing that loop. You don't need 3 labels and 3 buttons, and you don't need to load all 3 images immediately when the program starts either.

加载和显示图像的代码应该移到 next_img 函数中.您可以使用命令 panel['image'] = img 更新标签的图像.

The code that loads and displays the image should be moved into the next_img function. You can update the label's image with the command panel['image'] = img.

为了循环浏览图像列表,最简单的方法是使用迭代器.您可以通过调用将图像列表转换为 iteratorimages = iter(images).然后你可以使用 next 函数在需要时从迭代器中获取下一张图像.

In order to cycle through the list of images, it's easiest to use an iterator. You can turn your list of images into an iterator by calling images = iter(images). Then you can use the next function to get the next image from the iterator when you need it.

import tkinter as tk
from PIL import ImageTk, Image

win = tk.Tk()
win.geometry('800x500')  # set window size
win.resizable(0, 0)  # fix window

panel = tk.Label(win)
panel.pack()

images = ['01.jpg', '02.jpg', '03.jpg']
images = iter(images)  # make an iterator

def next_img():
    try:
        img = next(images)  # get the next image from the iterator
    except StopIteration:
        return  # if there are no more images, do nothing

    # load the image and display it
    img = Image.open(img)
    img = ImageTk.PhotoImage(img)
    panel.img = img  # keep a reference so it's not garbage collected
    panel['image'] = img

btn = tk.Button(text='Next image', command=next_img)
btn.pack()

# show the first image
next_img()

win.mainloop()

此代码将遍历图像一次,当到达最后一张图像时,按下一张图像"按钮将无效.如果你想绕到第一张图片,你可以 itertools.cycle 来创建一个无限循环的迭代器:

This code will loop through the images once, and when the last image is reached, pressing the "Next image" button will have no effect. If you want to wrap around to the first image, you can itertools.cycle to create an infinitely looping iterator instead:

images = itertools.cycle(images)

这篇关于如何使用 python Tkinter 迭代图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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