Mundo em Python

Iluminação da Lua

from tkinter import *
import ephem
from datetime import datetime, timedelta root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Iluminação da Lua") titulo = Label(
text="Iluminação da Lua",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.25, rely=0.05) # ---------------- Mês ----------------
texto_sub1 = Label(
text="Mês:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.3, rely=0.32) Mes = StringVar() Mes_entrada = Entry(
textvariable=Mes,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Mes_entrada.place(relx=0.45, rely=0.33, relwidth=0.25)
Mes_entrada.focus() # ---------------- Ano ----------------
texto_sub2 = Label(
text="Ano:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.3, rely=0.45) Ano = StringVar() Ano_entrada = Entry(
textvariable=Ano,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Ano_entrada.place(relx=0.45, rely=0.46, relwidth=0.25) # ---------------- Resultado com Scroll ----------------
frame_resultado = Frame(root)
frame_resultado.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) scroll = Scrollbar(frame_resultado)
scroll.pack(side=RIGHT, fill=Y) resultado_texto = Text(
frame_resultado,
font=("Arial", 12, "bold"),
bg="#cfe2f3",
yscrollcommand=scroll.set,
wrap=WORD
)
resultado_texto.pack(side=LEFT, fill=BOTH, expand=True) scroll.config(command=resultado_texto.yview) # ---------------- Funções ----------------
def limpar():
Ano_entrada.delete(0, END)
Mes_entrada.delete(0, END)
resultado_texto.delete("1.0", END) def app():
try:
ano = int(Ano.get())
mes = int(Mes.get()) # -------- VALIDAÇÃO --------
if mes < 1 or mes > 12:
resultado_texto.delete("1.0", END)
resultado_texto.insert(END, "Erro: o mês deve estar entre 1 e 12.")
return

if ano < 1:
resultado_texto.delete("1.0", END)
resultado_texto.insert(END, "Erro: ano inválido.")
return

# cria data inicial (já valida fevereiro 28/29 automaticamente)
data = datetime(ano, mes, 1) dias = [] while data.month == mes:
lua = ephem.Moon(data)
dias.append((data.day, lua.phase))
data += timedelta(days=1) resultado_texto.delete("1.0", END) for dia, fase in dias:
resultado_texto.insert(
END,
f"Dia {dia}: iluminação {fase:.2f}%\n"
) except ValueError:
resultado_texto.delete("1.0", END)
resultado_texto.insert(END, "Erro: insere apenas números válidos.") # ---------------- 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) root.mainloop()
0