0

I was creating a to-do list program for my school project and faced this error when implementing a timer into the code following is the code:

from tkinter import *
root =Tk()
root.geometry("500x500")
font_tuple=("Proxima Nova", 15, "bold")
timer_frame=Frame(root)
timer_frame.pack()
sec=StringVar()
mins=StringVar()
hrs=StringVar()
time_sec_list =Entry(timer_frame, text=sec, font=font_tuple, width=2)
time_sec_list.grid(row=0, column=4, padx=10)
time_min_list =Entry(timer_frame, font=font_tuple, width=2)
time_min_list.grid(row=0, column=3, padx=10)

time_hrs_list =Entry(timer_frame, font=font_tuple, width=2)
time_hrs_list.grid(row=0, column=2, padx=10)

enter_time=Label(timer_frame, font=font_tuple,text="Enter time:", bg="#E0B4A3")
enter_time.grid(row=0, column=1)
def timer1():
        try:
            times = int(str(time_hrs_list.get()))*3600+ int(str((time_min_list.get())))*60 + int(str(time_sec_list.get()))
            while times > -1:
                minute,second = (times // 60 , times % 60)
                hour =0
                if minute > 60:
                    hour , minute = (minute // 60 , minute % 60)
                sec.set(second)
                mins.set(minute)
                hrs.set(hour)
                mroot.update()
                time.sleep(1)
                if(times == 0):
                    time_sec_list.set('00')
                    time_min_list.set('00')
                    time_hrs_list.set('00')
                times -= 1
        except Exception as e:
            print(e)
start_timer_b=Button(timer_frame, text="Start timer", command=timer1, pady=10)
start_timer_b.grid(row=1, column=3)

can anybody pls help me with why I am unable to convert the type of the data all help is appreciated there are 2 problems I am facing with the following code :

  1. the StringVar() is not showing in the entry widget that is its not showing 00 when I set it to 00
  2. the timer is not working it is unable to identify that the data inputted into the entry widget is a number and can be converted into a string and is excepting it as a pure string with characters
hari
  • 35
  • 6
  • what exactly happens? does it print "value can't be converted to number" or what? also don't use a bare `except:`, at least have it as `except Exception as e:` and then also `print(e)` in the except block so you know what exact error was printed out, and also you shouldn't use either `for` or `while` loops as they block the `.mainloop` (and `.update` is suggested to be used (use `.after` "loops"), otherwise your question is unclear and also you don't need to convert the returned value form `.get` to a string as it is a string already, please provide a [mre] and specify the exact problem – Matiiss Oct 15 '21 at 10:46
  • "`.update` is _not_ suggested to be used" is what I meant – Matiiss Oct 15 '21 at 11:05
  • alright thank you for the suggestion but how should I replace the while loop from the code as it is necessary to update the screen after every second edit: ti have tried catching the exception just how you mentioned and the error it is showing is invalid literal for int() with base 10: – hari Oct 15 '21 at 18:28
  • well either have another thread that runs that loop or use `.after` "loops", also you still haven't explained the issue and haven't provided a [mre] – Matiiss Oct 15 '21 at 18:29
  • yeah just a second I am creating the minimal reproducible example I am pretty new to the platform so please forgive me for any mistakes – hari Oct 15 '21 at 18:34
  • could you explain what you mean by having another thread run the loop – hari Oct 15 '21 at 18:39
  • in this context I meant that you can use the [`threading`](https://docs.python.org/3/library/threading.html) module to run code in another thread (you can also simply search the internet for what threading is and you will get a way more detailed answer), also you haven't still provided a [mre] (read carefully what that is) – Matiiss Oct 15 '21 at 18:46
  • I have placed a minimal reproducible code I hope that helps in obtaining the solution – hari Oct 15 '21 at 18:59
  • Does this answer your question? [How can I schedule updates (f/e, to update a clock) in tkinter?](https://stackoverflow.com/questions/2400262/how-can-i-schedule-updates-f-e-to-update-a-clock-in-tkinter) – Matiiss Oct 15 '21 at 19:08
  • i am sorry to say but the answers that you have linked are not related to my problem. I believe I was still a bit unclear so my problem statement is that the user would enter the amount of time the timer should run for they would provide the input using the entry widget and when the button is pressed the timer starts. The answer given in the post u have linked shows the current time going on – hari Oct 16 '21 at 06:14

0 Answers0