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)
デモ
これは、あなたが得るものです:

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

こんにちは、それは私のために働いています。問題のあるサンプルコードを提供できますか?あるいは、キャンバスオブジェクトに対して 'update'を使ってみてください。 – VRage
なぜそれは動作していないと思いますか?これは正しい構文です。それは単に働いている必要があります。イメージアイテムを作成してすぐに削除します。それがうまくいかないという証拠はありますか? –