2016-05-02 30 views
0

私はフレームを作成し、Tkinterのキャンバスを使用してフレームにイメージを表示しています。しかし、私は連続して画像を連続して表示する必要があります。しかし、キャンバスをリフレッシュできませんでした。以下は私のコードです。Tkinterキャンバスリフレッシュ

cwgt=Canvas(self.parent.Frame1) 
cwgt.pack(expand=True, fill=BOTH) 
image1 = Image.open(image1) 
image1 = ImageTk.PhotoImage(image1) 
cwgt.img=image1 
cwgt.create_image(0, 0, anchor=NW, image=image1) 
cwgt.delete("all") 

cwgt.delete( "all")は機能しません。助けてください.......

+1

こんにちは、それは私のために働いています。問題のあるサンプルコードを提供できますか?あるいは、キャンバスオブジェクトに対して 'update'を使ってみてください。 – VRage

+1

なぜそれは動作していないと思いますか?これは正しい構文です。それは単に働いている必要があります。イメージアイテムを作成してすぐに削除します。それがうまくいかないという証拠はありますか? –

答えて

1

cwgt.delete( "all")は機能しません。助けてください........

この行は機能しませんが、他には何も働かないので、テキスト(コードではありません)それを達成する方法を説明します。

delete()メソッドは、実行したいことを実行します。文字列allを引数として渡して、Tkinter.Canvasウィジェットにあるすべてのアイテムを削除するか、消去したいアイテムへの参照を指定することができます。あなたはTkinter.Canvas上の一つの要素が複数ある場合は

全プログラム

''' 
Created on May 2, 2016 

@author: Billal Begueradj 
''' 
import Tkinter as Tk 
from PIL import Image, ImageTk 

class Begueradj(Tk.Frame): 
    ''' 
    Dislay an image on Tkinter.Canvas and delete it on button click 
    ''' 
    def __init__(self, parent): 
     ''' 
     Inititialize the GUI with a button and a Canvas objects 
     ''' 
     Tk.Frame.__init__(self, parent) 
     self.parent=parent 
     self.initialize_user_interface() 

    def initialize_user_interface(self): 
     """ 
     Draw the GUI 
     """ 
     self.parent.title("Billal BEGUERADJ: Image deletion")  
     self.parent.grid_rowconfigure(0,weight=1) 
     self.parent.grid_columnconfigure(0,weight=1) 
     self.parent.config(background="lavender")  

     # Create a button and append it a callback method to clear the image   
     self.deleteb = Tk.Button(self.parent, text = 'Delete', command = self.delete_image) 
     self.deleteb.grid(row = 0, column = 0) 

     self.canvas = Tk.Canvas(self.parent, width = 265, height = 200) 
     self.canvas.grid(row = 1, column = 0) 

     # Read an image from my Desktop 
     self.image = Image.open("/home/hacker/Desktop/homer.jpg") 
     self.photo = ImageTk.PhotoImage(self.image)   
     # Create the image on the Canvas  
     self.canvas.create_image(132,100, image = self.photo) 

    def delete_image(self): 
     ''' 
     Callback method to delete image 
     ''' 
     self.canvas.delete("all") 


# Main method 
def main(): 
    root=Tk.Tk() 
    d=Begueradj(root) 
    root.mainloop() 

# Main program  
if __name__=="__main__": 
    main() 

ウィジェットとあなただけの画像を削除したいTkinter.Canvas.create_image()を返すので、あなたはdelete()メソッドにそのIDを指定することができますidのイメージが作成されました(これは私がリンクしているドキュメントには記載されていません)。

self.ref_id = self.canvas.create_image(132,100, image = self.photo) 

をしてdelete_image()メソッド内::これは、

、上記のコードでは、あなたが実行することができます

self.canvas.delete(self.ref_id) 

デモ

これは、あなたが得るものです:

enter image description here

ボタンをクリックした後、イメージが一掃されます。

enter image description here