from tkinter import *
from datetime import datetime
from tkinter import messagebox
from functools import partial
# Janela principal
root = Tk()
root.geometry("700x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Sistema de login")
utilizadores = {
"joao": "1234",
"ana": "senha123",
"maria": "abc123",
}
# Título
titulo = Label(text="Sistema de login",
font=("Arial", 28, "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.35, rely=0.05)
# Labels dos campos
Label(text="Nome de utilizador:", font=("Arial", 18, "bold"),
bg="#103030", fg="#49e3e3").place(relx=0.27, rely=0.25)
Label(text="Senha:", font=("Arial", 18, "bold"),
bg="#103030", fg="#49e3e3").place(relx=0.48, rely=0.4)
Label(text="Data de Nascimento (DD-MM-YYYY):", font=("Arial", 18, "bold"),
bg="#103030", fg="#49e3e3").place(relx=0.05, rely=0.55)
# Entradas
Nome_utilizador = StringVar()
Entry(textvariable=Nome_utilizador, font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center').place(relx=0.65, rely=0.26, relwidth=0.28)
Senha = StringVar()
Entry(textvariable=Senha, font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center', show="*").place(relx=0.65, rely=0.41, relwidth=0.28)
Data_Nascimento = StringVar()
data_entry = Entry(textvariable=Data_Nascimento, font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center')
data_entry.place(relx=0.65, rely=0.56, relwidth=0.28)
# Função limpar
def limpar():
data_entry.delete(0, END)
Senha.set("")
Nome_utilizador.set("")
resultado_texto.config(text="")
# Função principal
def app():
try:
nome = Nome_utilizador.get().strip().lower()
senha = Senha.get().strip()
d = Data_Nascimento.get().strip()
# Verificar credenciais
if nome not in utilizadores or utilizadores[nome] != senha:
messagebox.showerror("Erro de Login", "Nome de utilizador ou senha incorretos.")
return
birth_date = datetime.strptime(d, "%d-%m-%Y")
today = datetime.today()
age = today.year - birth_date.year - (
(today.month, today.day) < (birth_date.month, birth_date.day))
if age < 18:
messagebox.showinfo("Informação Importante", "Ainda é menor. Não pode entrar na página.")
else:
abrir_calculadora()
except ValueError:
messagebox.showerror("Erro de data", "Data inválida! Use o formato DD-MM-YYYY.")
# Janela de calculadora de alcoolémia
def abrir_calculadora():
janela = Toplevel(root)
janela.geometry("700x700")
janela.config(bg="#304050")
janela.title("Calcular a alcoolémia ao longo do tempo")
genero = StringVar(value="Homem")
def store_genero(valor):
genero.set(valor)
def converter(rlabel, alcool_var, peso_var, tempo_var):
try:
alcool = float(alcool_var.get())
peso = float(peso_var.get())
tempo = float(tempo_var.get())
if alcool <= 0 or peso <= 0 or tempo < 0:
raise ValueError("Valores inválidos.")
taxa_elim = 0.015
if genero.get() == 'Homem':
resultado = (alcool / (peso * 0.68)) - (taxa_elim * tempo)
else:
resultado = (alcool / (peso * 0.55)) - (taxa_elim * tempo)
resultado = max(0, resultado)
rlabel.config(text=f"Sua alcoolémia atual é de aproximadamente {resultado:.3f} g/dL")
except ValueError:
rlabel.config(text="Erro: Insira apenas números válidos nos campos.")
# Labels
Label(janela, text="Quantas gramas de álcool você consumiu?", bg='#09A3BA', fg="#FFFFFF").place(relx=0.05, rely=0.1)
Label(janela, text="Qual é o seu peso em kg?", bg='#09A3BA', fg="#FFFFFF").place(relx=0.25, rely=0.2)
Label(janela, text="Há quantas horas você está consumindo álcool?", bg='#09A3BA', fg="#FFFFFF").place(relx=0.05, rely=0.3)
# Entradas
alcool_var = DoubleVar()
Entry(janela, textvariable=alcool_var).place(relx=0.65, rely=0.1)
peso_var = DoubleVar()
Entry(janela, textvariable=peso_var).place(relx=0.65, rely=0.2)
tempo_var = DoubleVar()
Entry(janela, textvariable=tempo_var).place(relx=0.7, rely=0.3, relwidth=0.25)
# Gênero
Label(janela, text="Gênero:", bg='#09A3BA', fg="#FFFFFF").place(relx=0.25, rely=0.45)
genero_menu = OptionMenu(janela, genero, "Homem", "Mulher", command=store_genero)
genero_menu.place(relx=0.4, rely=0.45)
genero_menu.config(bg='#09A3BA', fg="#FFFFFF")
genero_menu["menu"].config(bg='#09A3BA', fg="#FFFFFF")
# Resultado
result_label = Label(janela, bg='#09A3BA', fg="#FFFFFF")
result_label.place(relx=0.05, rely=0.8)
# Botão calcular
calcular_btn = Button(janela, text="Converter",
command=partial(converter, result_label, alcool_var, peso_var, tempo_var),
bg='#09A3BA', fg="#FFFFFF")
calcular_btn.place(relx=0.4, rely=0.6)
# Botões principais
Button(text="Confirmar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app).place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1)
Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar).place(relx=0.4, rely=0.65, relwidth=0.25, relheight=0.1)
Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy).place(relx=0.7, rely=0.65, relwidth=0.25, relheight=0.1)
# Resultado/Erros
resultado_texto = Label(text="", font=("Arial", 12, "bold"), bg="#103030", fg="white")
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
# Iniciar aplicação
root.mainloop()