1

I am trying to make a timer which counts down to a specified time says "Click me" deletes the prompt and then continues. I'm trying to use time.sleep to keep the prompt up for 1 second then delete it but it keeps giving me an error that neither string nor int have the command delete, destroy, etc. Does anyone know why this problem is occurring. Thanks for any help in advance

from datetime import datetime
from tkinter import *
import time
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()

selected_date = datetime(2017,3,24,22,22)
me = Label(tk, font=('Times',45))
me.place(relx=0.5, rely=0.5, anchor=CENTER)
me.pack()

def countdown() :

    s = (selected_date - datetime.now()).seconds 
    me['text']=str(s) + " seconds until click" 
    if s == 0:
         text1 = str(canvas.create_text(200, 200, text="CLICK NOW", font=('Times',45)))

         text1.delete()
         time.sleep(1)


    canvas.after(1000, countdown) 


canvas.after(1000, countdown) 
tk.mainloop()

Thanks for any help in advance. I am not sure why it continues to give me this error

falsetru
  • 357,413
  • 63
  • 732
  • 636
Kg123
  • 107
  • 1
  • 1
  • 11
  • I don't know tkinter, but it seems like you should be removing something from the canvas. `text1` is just a string representation of the canvas. – Barmar Mar 25 '17 at 02:30
  • Maybe just don't call `str()`. – Barmar Mar 25 '17 at 02:31
  • @Barmar. That is what I also tried but it still gives me an error – Kg123 Mar 25 '17 at 02:32
  • Possible duplicate of [How do you delete a create\_text in a canvas?](http://stackoverflow.com/questions/28840882/how-do-you-delete-a-create-text-in-a-canvas) – Barmar Mar 25 '17 at 02:33
  • citing that int doesn't have an attribute called delete – Kg123 Mar 25 '17 at 02:34

1 Answers1

1

str does not have delete. Even though it has one, because the str is immutable object, it will not change the string itself in-place.

  • You need to use me['text'] = '' to reset text to delete countdown message.

  • If you want to use delete CLICK NOW message, you need to save the return value of create_text, and pass it to canvas.delete method later.


def countdown() :
    s = (selected_date - datetime.now()).seconds 
    me['text']=str(s) + " seconds until click" 
    if s == 0:
        text1 = canvas.create_text(200, 200, text="CLICK NOW", font=('Times',45))
        me['text'] = ''  # to delete count down
        #### If you want to delete the 'CLICK NOW' message in a second,
        #### do the following
        # canvas.after(1000, lambda: canvas.delete(text1))
    else:
        canvas.after(1000, countdown)

BTW, you don't need to use time.sleep.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thanks so much. This example worked. Will it runs this everyday – Kg123 Mar 25 '17 at 02:41
  • @Kg123, runs this everyday? what does that mean? – falsetru Mar 25 '17 at 02:43
  • I made this program so it would run every single day for the same time and say "Click me" at 10:22. Will it say "click me" every day or no – Kg123 Mar 25 '17 at 02:44
  • @Kg123, It will not work because you hard-coded `selected_date = datetime(2017,3,24,22,22)`. You need to change the `selected_date` value in the if statement body accordingly. – falsetru Mar 25 '17 at 02:47
  • How would I change it to every day. – Kg123 Mar 25 '17 at 02:48
  • @Kg123, Could you post a separated question? I think the original question is answered, and I don't want to make lengthy comments here. – falsetru Mar 25 '17 at 02:49
  • Yes. I will tomorrow because I can't ask another for 90 minutes – Kg123 Mar 25 '17 at 02:51
  • I would have otherwise – Kg123 Mar 25 '17 at 02:51
  • @Kg123, `selected_date = datetime.now() + timedelta(days=1)` (you need to import `timedelta` from `datetime` module). Also you should declare `selected_date` a global variable to change global variable. – falsetru Mar 25 '17 at 02:52
  • How would I make it for everyday. Would it be days=x? – Kg123 Mar 25 '17 at 02:54
  • @Kg123, the function is called using `canvas.after(1000, ...)`. So it will run every seconds. `(days=1)` should be 1 to mean next day this time. – falsetru Mar 25 '17 at 02:55
  • @Kg123, Try `datetime.now()` and `datetime.now() + timedelta(days=1)` in python interactive shell. – falsetru Mar 25 '17 at 02:56