Mundo em Python

Converter a hora no formato 12 horas para formato 24 horas

# Terminal


def convert24(str1):
if str1[-2:] == "AM" and str1[:2] == "12":
return "00" + str1[2:-2]
elif str1[-2:] == "AM":
return str1[:-2]
elif str1[-2:] == "PM" and str1[:2] == "12":
return str1[:-2]
else:
return str(int(str1[:2]) + 12) + str1[2:8]
hora = str(input("Digite a Hora \033[7;36m(Exemplo 08)\033[m: "))
minuto= str(input("Digite os minutos \033[7;36m(Exemplo 08)\033[m:"))
segundos = str(input("Digite os segundos \033[7;36m (Exemplo 08)\033[m: "))
am_pm = str(input("Digite se é AM ou PM : ")).upper()
print("\n")
hora_total =f'{hora}:{minuto}:{segundos} {am_pm}'
print(f'A hora no formato 12 horas {hora_total}'
f' é no formato 24 horas {convert24(hora_total)}.')

# Tkinter


from tkinter import *
root = Tk()
root.title("Converter 12 horas")
root.geometry("500x500")
root.resizable(0,0) def app():
h = hora.get()
m = minutos.get()
s = segundos.get()
sig = am_pm.get().upper()
hora_total = f'{h}:{m}:{s} {sig}'

def convert24(hora_total):
if hora_total[-2:] == "AM" and hora_total[:2] == "12":
return "00" + hora_total[2:-2]
elif hora_total[-2:] == "AM":
return hora_total[:-2]
elif hora_total[-2:] == "PM" and hora_total[:2] == "12":
return hora_total[:-2]
else:
return str(int(hora_total[:2]) + 12) + hora_total[2:8]
resultado.set(convert24(hora_total))
root.configure(bg="#d3ffce")
titulo = Label(text="Converter formato 12 horas para formato 24 horas",bg="#d3ffce",
fg="#0b5394",font=("arial","13","bold"))
titulo.place(relx=0.1,rely=0.05)
hora_texto = Label(text="Hora (Exemplo 08)",bg="#d3ffce",
fg="#0b5394",font=("arial","14","bold"))
hora_texto.place(relx=0.2,rely=0.2)
hora = StringVar()
minutos = StringVar()
segundos = StringVar()
am_pm = StringVar() hora_entrada = Entry(textvariable=hora,font=("Arial","12","bold"),
bg="white",fg="red",justify='center')
hora_entrada.place(relx=0.65,rely=0.2,relwidth=0.2)
minutos_texto = Label(text="Minutos (Exemplo 08)",bg="#d3ffce",
fg="#0b5394",font=("arial","14","bold"))
minutos_texto.place(relx=0.15,rely=0.3) minutos_entrada = Entry(textvariable=minutos,font=("Arial","12","bold"),
bg="white",fg="red",justify='center')
minutos_entrada.place(relx=0.65,rely=0.3,relwidth=0.2) segundos_texto = Label(text="Segundos (Exemplo 08)",bg="#d3ffce",
fg="#0b5394",font=("arial","14","bold"))
segundos_texto.place(relx=0.12,rely=0.4) segundos_entrada = Entry(textvariable=segundos,font=("Arial","12","bold"),
bg="white",fg="red",justify='center')
segundos_entrada.place(relx=0.65,rely=0.4,relwidth=0.2)
am_pm_texto = Label(text="Tempo (AM ou PM)",bg="#d3ffce",
fg="#0b5394",font=("arial","14","bold"))
am_pm_texto.place(relx=0.2,rely=0.5) am_pm_entrada = Entry(textvariable=am_pm,font=("Arial","12","bold"),
bg="white",fg="red",justify='center')
am_pm_entrada.place(relx=0.65,rely=0.5,relwidth=0.2) but = Button( text="Converter", bd=2, bg='#107db2', fg='white',
font=('verdana', 14, 'bold'),command=app)
but.place(relx=0.35, rely=0.65, relwidth=0.3, relheight=0.1)
resultado = StringVar()
textofinal =Label(text="Formato em 24 horas",font=("Arial",14,"bold"),
bg="#d3ffce",fg="#ddb941")
textofinal.place(relx=0.3,rely=0.8,relwidth=0.4)
resulatdoresultado =Label(textvariable=resultado,font=("Arial",13,"bold")
,bg="#d3ffce",fg="#0f202f")
resulatdoresultado.place(relx=0.3,rely=0.9,relwidth=0.4)
root.mainloop()
0