在Python Tkinter中按顺序显示多个图像

2024-06-16 09:37:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个程序,显示一个初始图像,然后显示文件夹中最新的图像。在

除了Tkinter面板在窗口的第一个图像下面显示最新的图像外,我的一切都在工作。我想不出一个办法来摧毁原来的或做另一个没有旧图像的面板。我复制了下面的相关代码,不确定它是否会执行无错误。我的.py文件运行良好,没有任何错误,只有不需要的结果。任何帮助都会很好。在

from Tkinter import *
import Image, ImageTk
import datetime
import sys, os

FILE_PATH = "C:\\easy\\Pics"
#------------------------------------------------------------------------#
def quitX():
    root.destroy()
    sys.exit()

#------------------------------------------------------------------------#
def prg_task():
    # code section removed....

    # continue code

    im = Image.open("C:\easy\imageNEW.jpg")

    image1 = ImageTk.PhotoImage(im)

    # get the image size
    w = image1.width()
    h = image1.height()+10

    # position coordinates of root 'upper left corner'
    x = 500
    y = 200

    # make the root window the size of the image
    root.geometry("%dx%d+%d+%d" % (w, h, x, y))

    # root has no image argument, so use a label as a panel
    panel1 = Label(root, image=image1)
    panel1.pack(side='top', fill='both')

    # put a button on the image panel to test it
    button2 = Button(panel1, text='Close Window', command=quitX)
    button2.pack(side='bottom')

    # save the panel's image from 'garbage collection'
    panel1.image = image1

    root.after(10000, prg_task)

##########################################################################
# START CODE EXECUTION
# Initializing non-constant global variables

root = Tk()
root.title("Camera")
initIMG = Image.open("C:\easy\No-Image-Available.jpg")
tkimage = ImageTk.PhotoImage(initIMG)
panel1 = Label(root,image=tkimage)
panel1.pack(side='top', fill='both')
#------------------------------------------------------------------------#

# After 0.1s, attempt to grab still image from IP Camera
root.after(100, prg_task)
root.mainloop()

#------------------------------------------------------------------------#

Tags: thefrom图像imageimporttaskeasyroot
1条回答
网友
1楼 · 发布于 2024-06-16 09:37:10

如果一次只显示一个图像,则只需创建一次面板和标签。每次您想显示一个新图像时,创建该图像,然后执行类似于self.panel1.configure(image=new_image)的操作。在

相关问题 更多 >