from tkinter import *
def converter_armazenamento(valor, unidade_origem, unidade_destino):
fatores = {
'b': 1/8, # 1 bit = 1/8 byte
'B': 1, # 1 byte = 1 byte
'KB': 1024, # 1 KB = 1024 bytes
'MB': 1024**2, # 1 MB = 1024^2 bytes
'GB': 1024**3, # 1 GB = 1024^3 bytes
'TB': 1024**4, # 1 TB = 1024^4 bytes
'PB': 1024**5, # 1 PB = 1024^5 bytes
'EB': 1024**6 # 1 EB = 1024^6 bytes
}
valor_em_bytes = valor * fatores[unidade_origem]
resultado = valor_em_bytes / fatores[unidade_destino]
return resultado
root = Tk()
root.geometry("750x550")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Conversor de unidades de armazenamento")
titulo = Label(
root, text="Conversor de Unidades de Armazenamento",
font=("Arial", 25, "bold"), bg="#103030", fg="#49e3e3"
)
titulo.place(relx=0.05, rely=0.05)
Label(root, text="Unidade de origem:", font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3").place(relx=0.15, rely=0.25)
Label(root, text="Unidade do destino:", font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3").place(relx=0.15, rely=0.41)
Label(root, text="Valor:", font=("Arial", 18, "bold"), bg="#103030", fg="#49e3e3").place(relx=0.39, rely=0.53)
Valor = StringVar()
Valor_entrada = Entry(
root, textvariable=Valor, font=("Arial", 12, "bold"),
bg="white", fg="blue", justify='center'
)
Valor_entrada.place(relx=0.55, rely=0.54, relwidth=0.35)
Valor_entrada.focus()
var1 = StringVar()
unidades = ["b", "B", "KB","MB","GB","TB","PB","EB"]
dropdown1 = OptionMenu(root, var1, *unidades)
var1.set(unidades[0])
dropdown1.place(relx=0.55, rely=0.26, relwidth=0.35)
dropdown1.config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 15, "bold"))
dropdown1["menu"].config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 18, "bold"))
var2 = StringVar()
dropdown2 = OptionMenu(root, var2, *unidades)
var2.set(unidades[0])
dropdown2.place(relx=0.55, rely=0.41, relwidth=0.35)
dropdown2.config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 15, "bold"))
dropdown2["menu"].config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 18, "bold"))
def limpar():
Valor_entrada.delete(0, END)
resultado_texto.config(text="")
# --- Função Converter ---
def app():
try:
valor = float(Valor.get())
unidade_origem = var1.get()
unidade_destino = var2.get()
resultado = converter_armazenamento(valor, unidade_origem, unidade_destino)
mensagem = f"{valor} {unidade_origem} = {resultado:.6f} {unidade_destino}"
resultado_texto.config(text=mensagem)
except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos.")
Button(root, text="Converter", bd=2, bg='#107db2', fg='white', font=('verdana', 12, 'bold'), command=app).place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1)
Button(root, 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(root, 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)
resultado_texto = Label(
root, text="", font=("Arial", 12, "bold"),
bg="#103030", fg="#49e3e3", wraplength=700, justify="center"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
root.mainloop()