from tkinter import *
root = Tk()
root.geometry("500x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Preço por Litro")
titulo = Label(text="Preço por Litro",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.15, rely=0.05)
texto_sub1 = Label(text="Preço Total:",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.35, rely=0.2)
texto_sub2 = Label(text="Quantidade da embalagem:",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub2.place(relx=0.05, rely=0.3)
Preço_Total = StringVar()
Preço_Total_entrada = Entry(textvariable=Preço_Total,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Preço_Total_entrada.place(relx=0.7, rely=0.2, relwidth=0.25)
Quantidade = StringVar()
Quantidade_entrada = Entry(textvariable=Quantidade,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
Quantidade_entrada.place(relx=0.7, rely=0.31, relwidth=0.25)
def limpar():
Preço_Total_entrada.delete(0, END)
Quantidade_entrada.delete(0, END)
resultado_texto.config(text="")
def app():
try:
total = float(Preço_Total.get())
q = float(Quantidade.get())
var_tempo = var.get()
if q == 0:
resultado_texto.config(text="Erro: Quantidade não pode ser zero.")
return
if var_tempo == "Mililitros (ml)":
litros_convertido = q / 1000
elif var_tempo == "Centilitros (cl)":
litros_convertido = q / 100
elif var_tempo == "Litros (L)":
litros_convertido = q
else:
resultado_texto.config(text="Unidade inválida.")
return
preco_por_litro = total / litros_convertido
resultado_texto.config(text=f"Preço por litro: {round(preco_por_litro, 2)} €")
except ValueError:
resultado_texto.config(text="Erro: Insira valores numéricos válidos.")
var = StringVar()
dropDownList = ["Mililitros (ml)", "Centilitros (cl)", "Litros (L)"]
dropdown = OptionMenu(root, var, *dropDownList)
var.set(dropDownList[0])
dropdown.place(relx=0.2, rely=0.45, relwidth=0.6, relheight=0.1)
dropdown.config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", "15", "bold"))
dropdown["menu"].config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", "12", "bold"))
but1 = Button(text="Calcular", 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()