Mundo em Python

Calcular o Preço Grama (usando Tkinter)

from tkinter import *
root = Tk()
root.geometry("650x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Calcular o Preço Grama")
titulo = Label(
text="Calcular o Preço Grama",
font=("Arial", "28", "bold"),
bg="#103030",
fg="#49e3e3",
)
titulo.place(relx=0.15, rely=0.05) texto_sub1 = Label(
text="Preço por kg:",
font=("Arial", "25", "bold"),
bg="#103030",
fg="#49e3e3",
)
texto_sub1.place(relx=0.27, rely=0.32) texto_sub2 = Label(
text="Quantidades de gramas:",
font=("Arial", "25", "bold"),
bg="#103030",
fg="#49e3e3",
)
texto_sub2.place(relx=0.05, rely=0.5)
Preço_kg = StringVar()
Preço_kg_entrada = Entry(
textvariable=Preço_kg,
font=("Arial", "12", "bold"),
bg="white",
fg="blue",
justify="center",
)
Preço_kg_entrada.place(relx=0.65, rely=0.34, relwidth=0.25)
Preço_kg_entrada.focus() # Foco inicial no campo de preço por kg

Quantidades_gramas = StringVar()
Quantidades_gramas_entrada = Entry(
textvariable=Quantidades_gramas,
font=("Arial", "12", "bold"),
bg="white",
fg="blue",
justify="center",
)
Quantidades_gramas_entrada.place(relx=0.66, rely=0.51, relwidth=0.25)
def limpar():
Quantidades_gramas_entrada.delete(0, END)
Preço_kg_entrada.delete(0, END)
resultado_texto.config(text="")
Preço_kg_entrada.focus()
def app():
try:
gramas = float(Quantidades_gramas.get())
preço_por_kg = float(Preço_kg.get())
preço_total = (gramas / 1000) * preço_por_kg resultado_texto.config(
text=f"Preço para {gramas:.0f} gramas: R$ {preço_total:.2f}"
)
except ValueError:
resultado_texto.config(text="Valor digitado de forma inválida!") 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",
anchor="center",
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) root.mainloop()
0