python - Is there a way to clear the canvas if using tkinter and writing on it again with a different message? -


i have created game uses tkinter open gui. has timer , when timer runs out want clear gui , have message example: time up, scored + there score. have been able make label on gui stays there if timer doesnt = 0. here source code:

import tkinter import random  colours = ['red','blue','green','pink','black','yellow','orange','white','purple','brown']  score=0 timeleft=30 def startgame(event):     if timeleft == 30:         countdown()      nextcolour()  def nextcolour():     global score     global timeleft      if timeleft > 0:     e.focus_set()      if e.get().lower() == colours[1].lower():         score += 1     e.delete(0, tkinter.end)     random.shuffle(colours)     label.config(fg=str(colours[1]), text=str(colours[0]))     scorelabel.config(text="score: " + str(score))  def countdown():     global timeleft      if timeleft > 0:         timeleft -= 1         timelabel.config(text="time left: " + str(timeleft))         timelabel.after(1000, countdown)     if timeleft == 0:         canvas.delete("all")         endgame.config(text="time up, scored " + str(score))   root = tkinter.tk() root.title("ttcantw") root.geometry("375x250")  instructions = tkinter.label(root, text="type in colour of words,     , not word text!", font=('ariel', 12)) instructions.pack()  endgame = tkinter.label(root, text="time up, scored " + str(score), font=('ariel', 12)) endgame.pack()  scorelabel = tkinter.label(root, text="press enter start", font=   ('helvetica', 12)) scorelabel.pack()  timelabel = tkinter.label(root, text="time left: " + str(timeleft), font=('helvetica', 12)) timelabel.pack()   label = tkinter.label(root, font=('helvetica', 60)) label.pack()   e = tkinter.entry(root)  root.bind('<return>', startgame) e.pack() e.focus_set() root.mainloop() 

thanks in advance

you don't have canvas widget, impossible delete have use destroy delete every widgets want delete.. edited function

def countdown():     global timeleft      if timeleft > 0:         timeleft -= 1         timelabel.config(text="time left: " + str(timeleft))         timelabel.after(1000, countdown)      if timeleft == 0:         #canvas.delete("all")         instructions.destroy()         timelabel.destroy()         label.destroy()         e.destroy()         #-------------------------         endgame.config(text="time up, scored " + str(score)) 

Comments