Reputation: 11
import tkinter
from tkinter import font
def deleteButton(event):
canvas.delete("tag1")
main = tkinter.Tk()
main.geometry("1280x720+320+180")
main.resizable(0,0)
cv = tkinter.Canvas(main, bg = "turquoise", width = 200, height = 200)
cv.create_polygon(400, 400, 400, 400, fill = "turquoise", tag = "tag1")
cv.place(x = 300, y = 300)
cv.pack()
start_button = tkinter.Button(main, text = "Play!", command = deleteButton, fg =` `"white", bg = "#0A9AFF", relief = "flat")
start_button.place(x = 625, y = 300)
main.mainloop()
when run this code, I get
"TypeError: deleteButton() missing 1 required positional argument: 'event'."
I've already tried to .bind
but I couldn't fix it.
I want to delete canvas with pushing button, how can I do that?
Starting new line doesn't work. Thank you
Upvotes: 0
Views: 3753
Reputation: 386342
When you use bind
, it automatically passes a parameter that represents the event. Therefore, a function needs to accept this parameter. When you use command=
, there is no event parameter, therefore your function shouldn't accept one.
As for the error "NameError: name 'canvas' is not defined"
, that's because you've named your canvas cv
, not canvas
. To delete the items on your specific canvas it would be cv.delete(...)
, not canvas.delete(...)
.
Upvotes: 0
Reputation: 11
To clear a canvas, use the delete method. Give it the special parameter "all" to delete all items on the canvas (the string "all"" is a special tag that represents all items on the canvas):
canvas.delete("all")
If you want to delete certain items on the canvas (such as foreground objects, while leaving the background objects on the display) you can assign tags to each item. Then, instead of "all", you could supply the name of a tag.
Upvotes: 1