from tkinter import *
root = Tk()
root.geometry("700x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Simulador de Preço da Gasolina")
# ===================== TÍTULO =====================
titulo = Label(
text="Simulador de Preço da Gasolina",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.08, rely=0.05)
# ===================== TEXTOS =====================
Label(
text="Preço do barril (€):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
).place(relx=0.35, rely=0.25)
Label(
text="Custos (refinação + distribuição) €/L:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
).place(relx=0.05, rely=0.4)
Label(
text="Impostos €/L:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
).place(relx=0.43, rely=0.53)
# ===================== VARIÁVEIS =====================
preco_barril = StringVar()
custos = StringVar()
impostos = StringVar()
# ===================== ENTRADAS =====================
entrada_preco = Entry(
textvariable=preco_barril,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
entrada_preco.place(relx=0.68, rely=0.26, relwidth=0.3)
entrada_preco.focus()
entrada_custos = Entry(
textvariable=custos,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
entrada_custos.place(relx=0.68, rely=0.41, relwidth=0.3)
entrada_impostos = Entry(
textvariable=impostos,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
entrada_impostos.place(relx=0.68, rely=0.53, relwidth=0.3)
# ===================== RESULTADO =====================
resultado_texto = Label(
text="",
font=("Arial", 14, "bold"),
bg="#cfe2f3"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
# ===================== FUNÇÕES =====================
def limpar():
entrada_impostos.delete(0, END)
entrada_custos.delete(0, END)
entrada_preco.delete(0, END)
resultado_texto.config(text="")
def limpar_resultado(*args):
resultado_texto.config(text="")
def calcular():
try:
imp = float(impostos.get())
cus = float(custos.get())
barril = float(preco_barril.get())
if imp < 0 or cus < 0 or barril < 0:
resultado_texto.config(text="Os valores não podem ser negativos.", fg="red")
return
# cálculo com eficiência do barril
preco_litro = (barril / (159 * 0.85)) + cus + imp
if preco_litro < 1.5:
avaliacao = "Preço baixo"
cor = "green"
elif preco_litro < 2:
avaliacao = "Preço normal"
cor = "orange"
else:
avaliacao = "Preço alto"
cor = "red"
mensagem = f"Preço estimado: {preco_litro:.2f} €/L\n{avaliacao}"
resultado_texto.config(text=mensagem, fg=cor)
except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos.", fg="red")
# atualizar automaticamente resultado ao escrever
preco_barril.trace_add("write", limpar_resultado)
custos.trace_add("write", limpar_resultado)
impostos.trace_add("write", limpar_resultado)
# ===================== BOTÕES =====================
Button(
text="Calcular",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=calcular
).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)
# ===================== LOOP =====================
root.mainloop()