Mundo em Python

Conversor de Tamanho de Ficheiros

from tkinter import *

root = Tk()
root.geometry("700x600")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Conversor de Tamanho de Ficheiros") # Título
titulo = Label(
text="Conversor de Tamanho de Ficheiros",
font=("Arial", 28, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.05, rely=0.05) # Texto valor
texto_sub1 = Label(
text="Valor a converter:",
font=("Arial", 20, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.1, rely=0.2) Valor_converter = StringVar()
Valor_converter_entrada = Entry(
textvariable=Valor_converter,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Valor_converter_entrada.place(relx=0.5, rely=0.21, relwidth=0.48)
Valor_converter_entrada.focus() # Unidade de origem
texto_sub2 = Label(
text="Unidade de origem:",
font=("Arial", 20, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.08, rely=0.35) var1 = StringVar()
dropDownList1 = ["B", "KB", "MB", "GB"]
dropdown1 = OptionMenu(root, var1, *dropDownList1)
var1.set(dropDownList1[0])
dropdown1.place(relx=0.55, rely=0.35, relwidth=0.25)
dropdown1.config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 20, "bold"))
dropdown1["menu"].config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 20, "bold")) # Unidade de destino
texto_sub3 = Label(
text="Unidade de destino:",
font=("Arial", 20, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub3.place(relx=0.08, rely=0.5) var2 = StringVar()
dropDownList2 = ["B", "KB", "MB", "GB"]
dropdown2 = OptionMenu(root, var2, *dropDownList2)
var2.set(dropDownList2[0])
dropdown2.place(relx=0.55, rely=0.51, relwidth=0.25)
dropdown2.config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 20, "bold"))
dropdown2["menu"].config(background='#09A3BA', foreground="#FFFFFF", font=("Arial", 20, "bold")) # Função limpar
def limpar():
Valor_converter_entrada.delete(0, END)
resultado_texto.config(text="") # Função converter
def app():
try:
if Valor_converter.get() == "":
resultado_texto.config(text="Insira um valor para converter.")
return

Valor = float(Valor_converter.get())
unidade_origem = var1.get()
unidade_destino = var2.get() unidades = {
"B": 1,
"KB": 1024,
"MB": 1024 ** 2,
"GB": 1024 ** 3
} valor_em_bytes = Valor * unidades[unidade_origem]
valor_convertido = valor_em_bytes / unidades[unidade_destino] resultado_texto.config(
text=f"Resultado: {valor_convertido:.4f} {unidade_destino}"
) except ValueError:
resultado_texto.config(text="Por favor, insira valores numéricos válidos.") # Botões
but1 = Button(
text="Converter",
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", 14, "bold"),
bg="#cfe2f3",
fg="black",
anchor="center"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) root.mainloop()
0