from tkinter import *
# Criação da janela principal
root = Tk()
root.geometry("400x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Converter AM/PM para 24h e vice-versa")
# Título
titulo = Label(text="Converter AM/PM para 24h e vice-versa",
font=("Arial", "15", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.03, rely=0.05)
# Texto Subtítulo
texto_sub1 = Label(text="Digite o horário (12h com AM/PM ou 24h)",
font=("Arial", "13", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.08, rely=0.2)
# Entrada de Horário
hora = StringVar()
hora_entrada = Entry(textvariable=hora,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
hora_entrada.place(relx=0.1, rely=0.28, relwidth=0.8)
# Função para limpar a entrada
def limpar():
hora_entrada.delete(0, END)
resultado_texto.config(text="")
# Função para validar a entrada
def validar_hora(hora_entrada):
# Verifica se a entrada está vazia
if not hora_entrada:
return False
# Verifica se está no formato AM/PM
if "AM" in hora_entrada.upper() or "PM" in hora_entrada.upper():
hora_formatada = hora_entrada[:-2].strip()
if len(hora_formatada.split(":")) != 2:
return False
horas, minutos = hora_formatada.split(":")
return horas.isdigit() and minutos.isdigit() and 0 <= int(horas) <= 12 and 0 <= int(minutos) < 60
# Verifica se está no formato 24h
else:
if len(hora_entrada.split(":")) != 2:
return False
horas, minutos = hora_entrada.split(":")
return horas.isdigit() and minutos.isdigit() and 0 <= int(horas) < 24 and 0 <= int(minutos) < 60
# Função para converter horário de 12h AM/PM para 24h
def converter_para_24h(hora_12h):
if "AM" in hora_12h.upper():
if hora_12h.startswith("12"):
return "00" + hora_12h[2:-2]
return hora_12h[:-2]
elif "PM" in hora_12h.upper():
if hora_12h.startswith("12"):
return hora_12h[:-2]
else:
return str(int(hora_12h[:2]) + 12) + hora_12h[2:-2]
return "Formato inválido!"
# Função para converter horário de 24h para 12h AM/PM
def converter_para_12h(hora_24h):
if int(hora_24h[:2]) < 12:
return hora_24h + " AM"
elif int(hora_24h[:2]) == 12:
return hora_24h + " PM"
else:
return str(int(hora_24h[:2]) - 12) + hora_24h[2:] + " PM"
# Função principal do aplicativo
def app():
entrada = hora.get().strip()
if not validar_hora(entrada):
resultado_texto.config(text="Erro: Formato inválido!")
return
if "AM" in entrada.upper() or "PM" in entrada.upper():
resultado = converter_para_24h(entrada)
resultado_texto.config(text=f"Formato 24h: {resultado}")
else:
resultado = converter_para_12h(entrada)
resultado_texto.config(text=f"Formato 12h: {resultado}")
# Botão para calcular
but1 = Button(text="Calcular", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1)
# Botão para limpar
but_limpar = Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.4, rely=0.65, relwidth=0.25, relheight=0.1)
# Botão para sair
but_sair = Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.7, rely=0.65, relwidth=0.25, relheight=0.1)
# Label para mostrar o resultado
resultado_texto = Label(text="",
font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
# Iniciar o loop principal da interface
root.mainloop()