1

最初の画像を表示してから、フォルダ内の最新の画像を表示するプログラムを作成したいと思います。

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()

#------------------------------------------------------------------------#
4

1 に答える 1

1

一度に1つの画像のみを表示する場合は、パネルとラベルを1回だけ作成する必要があります。新しい画像を表示するたびに、画像を作成してから、のような操作を行いますself.panel1.configure(image=new_image)

于 2012-12-08T21:38:53.657 に答える