Mundo em Python

Divisor de Conta (Bill Splitter)

from tkinter import *

root = Tk()
root.geometry("700x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Divisor de Conta (Bill Splitter)") # Título
titulo = Label(
text="Divisor de Conta (Bill Splitter)",
font=("Arial", "28", "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.12, rely=0.05) # Labels
texto_sub1 = Label(
text="Valor total da conta (€):",
font=("Arial", "18", "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.1, rely=0.25) texto_sub2 = Label(
text="Número de pessoas:",
font=("Arial", "18", "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.14, rely=0.38) texto_sub3 = Label(
text="Gorjeta (%):",
font=("Arial", "18", "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub3.place(relx=0.28, rely=0.5) # Entradas
Valor_total_da_conta = StringVar()
Valor_total_da_conta_entrada = Entry(
textvariable=Valor_total_da_conta,
font=("Arial", "12", "bold"),
bg="white",
fg="blue",
justify='center'
)
Valor_total_da_conta_entrada.place(relx=0.55, rely=0.26, relwidth=0.35)
Valor_total_da_conta_entrada.focus() Número_pessoas = StringVar()
Número_pessoas_entrada = Entry(
textvariable=Número_pessoas,
font=("Arial", "12", "bold"),
bg="white",
fg="blue",
justify='center'
)
Número_pessoas_entrada.place(relx=0.55, rely=0.39, relwidth=0.35) Gorjeta = StringVar()
Gorjeta_entrada = Entry(
textvariable=Gorjeta,
font=("Arial", "12", "bold"),
bg="white",
fg="blue",
justify='center'
)
Gorjeta_entrada.place(relx=0.55, rely=0.51, relwidth=0.35) # Limpar
def limpar():
Gorjeta_entrada.delete(0, END)
Número_pessoas_entrada.delete(0, END)
Valor_total_da_conta_entrada.delete(0, END)
resultado_texto.delete("1.0", END) # Função principal
def app():
try:
# gorjeta opcional
if Gorjeta.get() == "":
gorjeta = 0
else:
gorjeta = float(Gorjeta.get()) pessoas = int(Número_pessoas.get())
total = float(Valor_total_da_conta.get()) if pessoas <= 0:
resultado_texto.delete("1.0", END)
resultado_texto.insert(END, "Erro: o número de pessoas deve ser maior que zero")
return

tip_value = total * (gorjeta / 100)
total_with_tip = total + tip_value
per_person = total_with_tip / pessoas mensagem = (
f"--- Resultado ---\n"
f"Conta base: {total:.2f}\n"
f"Gorjeta: {tip_value:.2f}\n"
f"Total com gorjeta: {total_with_tip:.2f}\n"
f"Cada pessoa paga: {per_person:.2f} €"
) resultado_texto.delete("1.0", END)
resultado_texto.insert(END, mensagem) except ValueError:
resultado_texto.delete("1.0", END)
resultado_texto.insert(END, "Erro: por favor insere números válidos") # Botões
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 com scroll (place)
frame_resultado = Frame(root, bg="#cfe2f3")
frame_resultado.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) scrollbar = Scrollbar(frame_resultado)
scrollbar.place(relx=0.97, rely=0, relwidth=0.03, relheight=1) resultado_texto = Text(
frame_resultado,
font=("Arial", 12, "bold"),
bg="#cfe2f3",
wrap=WORD,
yscrollcommand=scrollbar.set
)
resultado_texto.place(relx=0, rely=0, relwidth=0.97, relheight=1) scrollbar.config(command=resultado_texto.yview) root.mainloop()
0