from tkinter import *
# Janela
root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Calculadora de uso de armazenamento")
# Título
titulo = Label(
text="Calculadora de uso de armazenamento",
font=("Arial", 25, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.05, rely=0.05)
# Labels
texto_sub1 = Label(
text="Espaço total (GB):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.15, rely=0.25)
texto_sub2 = Label(
text="Espaço usado (GB):",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.13, rely=0.45)
# Variáveis
espaco_total = StringVar()
espaco_usado = StringVar()
# Entradas
espaco_total_entrada = Entry(
textvariable=espaco_total,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
espaco_total_entrada.place(relx=0.5, rely=0.26, relwidth=0.35)
espaco_total_entrada.focus()
espaco_usado_entrada = Entry(
textvariable=espaco_usado,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
espaco_usado_entrada.place(relx=0.5, rely=0.46, relwidth=0.35)
# Função calcular
def app():
try:
usado = float(espaco_usado.get())
total = float(espaco_total.get())
if total == 0:
resultado_texto.config(
text="Erro: o espaço total não pode ser 0",
bg="#ff4d4d"
)
return
percent_usado = (usado / total) * 100
percent_livre = 100 - percent_usado
# Definir cor e alerta
if percent_usado >= 90:
cor = "#ff4d4d" # vermelho
alerta = "Disco quase cheio!"
elif percent_usado >= 75:
cor = "#ffd11a" # amarelo
alerta = "Atenção ao espaço"
else:
cor = "#70db70" # verde
alerta = ""
mensagem = (
f"Usado: {round(percent_usado,2)}% | "
f"Livre: {round(percent_livre,2)}% {alerta}"
)
resultado_texto.config(text=mensagem, bg=cor)
except ValueError:
resultado_texto.config(
text="Por favor, insira valores numéricos válidos",
bg="#ff4d4d"
)
# Função limpar
def limpar():
espaco_total.set("")
espaco_usado.set("")
resultado_texto.config(text="", bg="#cfe2f3")
# 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
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)
# Loop
root.mainloop()