Countdown using tkinter

2

I'm trying to design a program on tkinter to do a countdown.

I can not understand why I can not get Entry and turn it into an integer. (As this is just a test, I did it using Portuguese myself)

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()

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

    
asked by anonymous 26.06.2016 / 21:13

3 answers

1

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

fromtkinterimport*root=Tk()deftick():sec=int(inicio.get())ifsec==0:time['text']='TEMPOESGOTADO'else:sec=sec-1time['text']=sectime.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()
    
26.06.2016 / 22:15
1

There is another solution, if you want to make more than one count during the execution 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) / p>

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

The "sec" variable is basically used to pass the time value to the next time the "tick" function is called recursively, and its default value is passed as "None" because before the button being clicked does not exist any time.

I hope I have helped!

    
27.06.2016 / 20:37
0

Your 'sec' variable is always getting the value that is in the input, so it looks like 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()

In this way you will only read the input value the first time

    
27.06.2016 / 20:18