Countdown by using Tkinter

Asked

Viewed 1,598 times

3

I’m trying to come up with a Tkinter show to count backwards.

I can’t understand why I can’t take Entry and turn it into an integer. (As this is just a test, I did using Portuguese same)

from tkinter import*

root = Tk()

inicio = Entry(root)
#adicionei esta linha abaixo
print(inicio.get())
sec = int(inicio.get())

def tick():
    global sec
    if sec == 0:
        time['text'] = 'TEMPO ESGOTADO'
    else:
        sec = sec - 1
        time['text'] = sec
        time.after(1000, tick)

label = Label(root, text="Quanto tempo você tem para realizar suas tarefas?")
label.grid(row=0, column=0)
inicio.grid(row=1, column=0)
time = Label(root, fg='green')
time.grid(row=2, column=0)
Button(root, fg='blue', text='Start', command=tick).grid(row=3, column=0)

root.mainloop()

inserir a descrição da imagem aqui

Only one blank space has been printed, as you can see.

  • Young man, what is the problem that occurs?

  • line 6, in <module> sec = int(start.get()) Valueerror: invalid literal for int() with base 10: ' '

  • Has how you print the value you are trying to convert and post it here?

  • I’m not trying to convert anything yet, the program doesn’t open... it’s just in the terminal

  • Orra, young man. That sec = int(inicio.get()) is an attempt at conversion. Do print(inicio.get()) and post what is shown on the screen.

  • pardon for ignoring... I am beginner. I added the line and the terminal continues giving the same message

  • Obviously it continues with the same error message, print only serves to display a certain value in the default output. What was the value shown?

  • Okay, your question is answered. "Space" is not a valid number, as it would be possible to convert a space to a number?

Show 3 more comments

3 answers

1

Got it! Thanks jbueno, I decided to put the get() inside the function. so it did not catch the blank entry (the space). But now the count does not continue, for example, if I put 60 in Entry, it goes to 59 and does not continue, freezing in 59. Do you have any idea how to solve?

inserir a descrição da imagem aqui

from tkinter import*

root = Tk()

def tick():
    sec = int(inicio.get())
    if sec == 0:
        time['text'] = 'TEMPO ESGOTADO'
    else:
        sec = sec - 1
        time['text'] = sec
        time.after(1000, tick)

label = Label(root, text="Quanto tempo você tem para realizar suas  tarefas?")
label.grid(row=0, column=0)
inicio = Entry(root, textvariable=0)
inicio.grid(row=1, column=0)
time = Label(root, fg='green')
time.grid(row=2, column=0)
Button(root, fg='blue', text='Start', command=tick).grid(row=3, column=0)

root.mainloop()

1

There is another solution, if you want to make more than one count during the implementation of the program (in the solution proposed by Fabiano the "Entry" will be read only once):

from tkinter import*

root = Tk()

def tick(validador = False,sec = None):
    if validador == False:
        sec = int(inicio.get())
    if sec == 0:
        time['text'] = 'TEMPO ESGOTADO'
    else:
        sec = sec - 1
        time['text'] = sec
        time.after(1000, lambda : tick(True,sec))


label = Label(root, text="Quanto tempo você tem para realizar suas  tarefas?")
label.grid(row=0, column=0)
inicio = Entry(root, textvariable=0)
inicio.grid(row=1, column=0)
time = Label(root, fg='green')
time.grid(row=2, column=0)
Button(root, fg='blue', text='Start', command=tick).grid(row=3, column=0)

root.mainloop()

In the "after" function it is not possible to call functions with parameters, so the use of lambda which basically returns the "tick" function with the parameters (this task can also be done by the partial function of the itertools module).

The variable "validator" basically vericifica if the function is called recursive mode or through the click on the button, note that when the function "tick" is called by the click of the button it does it with the parameters in the "default" values, in case "validator" == "False". Therefore, the first "if" checks the value of "validator" if "False" captures the text of the variable "start".

Already the variable "sec" is basically used to pass the value of time to the next time the function "tick" is called recursive mode, in addition its default value is passed as "None" because before the button is clicked there is no time.

I hope I’ve helped!

  • worked well, thanks! I could just explain the changes (for what purpose the validator; sec=None; and the changes in . after, like lambda use : tick(True, sec))?

  • I made a small explanation, if you do not understand.

0

Your 'sec' variable is always taking the value that is in the input, so it seems that it is not updating.

To illustrate, I started the variable as global. See:

from Tkinter import*

root = Tk()

sec = None

def tick():
    global sec
    if sec == None:
        sec = int(inicio.get())
    if sec == 0:
        time['text'] = 'TEMPO ESGOTADO'
    else:
        sec = sec - 1
        time['text'] = sec
        time.after(1000, tick)

label = Label(root, text="Quanto tempo você tem para realizar suas  tarefas?")
label.grid(row=0, column=0)
inicio = Entry(root, textvariable=0)
inicio.grid(row=1, column=0)
time = Label(root, fg='green')
time.grid(row=2, column=0)
Button(root, fg='blue', text='Start', command=tick).grid(row=3, column=0)

root.mainloop()

This way you will only read the value of the input in the first time

Browser other questions tagged

You are not signed in. Login or sign up in order to post.