Mundo em Python

Contagem decrescente e crescente

from tkinter import *
minutos = 4
segundos = 0
pausado = False
def formatar_tempo(minutos, segundos):
return f"{minutos:02}:{segundos:02}"
def contagem_decrescente():
global minutos, segundos, pausado
if pausado:
return
if minutos == 0 and segundos == 0:
contagem_crescente(0, 1)
else:
texto_sub1.config(text=formatar_tempo(minutos, segundos))
segundos -= 1
if segundos < 0:
minutos -= 1
segundos = 59
root.after(2000, contagem_decrescente) def contagem_crescente(min, seg):
global pausado if pausado:
return
texto_sub1.config(text=formatar_tempo(min, seg))
seg += 1
if seg == 60:
min += 1
seg = 0
root.after(1000, contagem_crescente, min, seg) def pausar():
global pausado
pausado = True

def retomar():
global pausado
pausado = False
if minutos == 0 and segundos == 0:
contagem_crescente(0, 1)
else:
contagem_decrescente()
def reiniciar():
global minutos, segundos, pausado
pausado = False
minutos, segundos = 4, 0
texto_sub1.config(text=formatar_tempo(minutos, segundos))
contagem_decrescente()
root = Tk()
root.geometry("700x300")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Contagem decrescente e crescente")
titulo = Label(root, text="Contagem decrescente e crescente",
font=("Arial", 22, "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.15, rely=0.05)
texto_sub1 = Label(root, text="04:00",
font=("Arial", 40, "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.4, rely=0.32)
btn_pausar = Button(root, text="Pausar", font=("Arial", 14, "bold"),
bg="#ffcc00", fg="black", command=pausar)
btn_pausar.place(relx=0.2, rely=0.6) btn_retomar = Button(root, text="Retomar", font=("Arial", 14, "bold"),
bg="#00cc66", fg="white", command=retomar)
btn_retomar.place(relx=0.4, rely=0.6) btn_reiniciar = Button(root, text="Reiniciar", font=("Arial", 14, "bold"),
bg="#cc0000", fg="white", command=reiniciar)
btn_reiniciar.place(relx=0.6, rely=0.6)
contagem_decrescente()
root.mainloop()
0