Mundo em Python

Converte 12h → 24h e 24h → 12h

from tkinter import *

def para_24h(horario):
hora, periodo = horario.split()
h, m = map(int, hora.split(":")) if periodo.upper() == "AM":
if h == 12:
h = 0
else: # PM
if h != 12:
h += 12

return f"{h:02d}:{m:02d}"

def para_12h(horario):
h, m = map(int, horario.split(":")) if h == 0:
return f"12:{m:02d} AM"
elif h < 12:
return f"{h:02d}:{m:02d} AM"
elif h == 12:
return f"{h:02d}:{m:02d} PM"
else:
return f"{h-12:02d}:{m:02d} PM"


root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Converte 12h → 24h e 24h → 12h") titulo = Label(
text="Converte 12h → 24h e 24h → 12h",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.08, rely=0.05) texto_sub1 = Label(
text="Digite a hora (ex: 03:20 PM ou 18:30):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.05, rely=0.32) hora = StringVar()
hora_entrada = Entry(
textvariable=hora,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
hora_entrada.place(relx=0.68, rely=0.33, relwidth=0.3)
hora_entrada.focus()
def limpar():
hora_entrada.delete(0, END)
resultado_texto.config(text="") def app():
try:
Hora = hora.get().strip() if "AM" in Hora.upper() or "PM" in Hora.upper():
mensagem = f"Formato 12h → 24h: {para_24h(Hora)}"
else:
mensagem = f"Formato 24h → 12h: {para_12h(Hora)}"

resultado_texto.config(text=mensagem) except ValueError:
resultado_texto.config(text="Por favor, insira uma hora válida.")
# --- BOTÕES ---
but1 = Button(
text="Mostrar",
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) 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) 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) 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) root.mainloop()
0