from tkinter import *
root = Tk()
root.geometry("700x500")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Cifra Binária")
titulo = Label(
root,
text="Cifra Binária",
font=("Arial", 30, "bold"),
bg="#103030",
fg="#49e3e3"
)
titulo.place(relx=0.35, rely=0.05)
texto_sub1 = Label(
root,
text="Texto:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub1.place(relx=0.1, rely=0.25)
Texto = StringVar()
Texto_entrada = Entry(
root,
textvariable=Texto,
font=("Arial", 12, "bold"),
bg="white",
fg="blue",
justify='center'
)
Texto_entrada.place(relx=0.23, rely=0.26, relwidth=0.73)
Texto_entrada.focus()
# Opção codificar/descodificar
var = StringVar()
texto_sub2 = Label(
root,
text="Escolha a opção:",
font=("Arial", 18, "bold"),
bg="#103030",
fg="#49e3e3"
)
texto_sub2.place(relx=0.1, rely=0.35)
dropDownList = ["Codificar", "Descodificar"]
dropdown = OptionMenu(root, var, *dropDownList)
var.set(dropDownList[0])
dropdown.place(relx=0.5, rely=0.33, relwidth=0.4)
dropdown.config(
background='#09A3BA',
foreground="#FFFFFF",
font=("Arial", 20, "bold")
)
dropdown["menu"].config(
background='#09A3BA',
foreground="#FFFFFF",
font=("Arial", 20, "bold")
)
# Label resultado
resultado_texto = Label(
root,
text="",
font=("Arial", 12, "bold"),
bg="#cfe2f3"
)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)
def limpar():
Texto_entrada.delete(0, END)
resultado_texto.config(text="")
def copiar_resultado():
texto = resultado_texto.cget("text")
if texto != "":
root.clipboard_clear()
root.clipboard_append(texto)
root.update()
resultado_texto.config(text="Resultado copiado!")
def app():
text = Texto.get()
v = var.get()
if text == "":
resultado_texto.config(text="O texto está vazio!")
return
if v == "Codificar":
binario = ""
for letra in text:
binario += format(ord(letra), '08b') + " "
resultado = binario.strip()
else:
try:
texto = ""
for bloco in text.split():
texto += chr(int(bloco, 2))
resultado = texto
except:
resultado = "Binário inválido!"
resultado_texto.config(text=resultado)
but1 = Button(
root,
text="Mostrar",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=app
)
but1.place(relx=0.1, rely=0.5, relwidth=0.25, relheight=0.1)
but_limpar = Button(
root,
text="Limpar",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=limpar
)
but_limpar.place(relx=0.4, rely=0.5, relwidth=0.25, relheight=0.1)
but_sair = Button(
root,
text="Sair",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=root.destroy
)
but_sair.place(relx=0.7, rely=0.5, relwidth=0.25, relheight=0.1)
but_copiar = Button(
root,
text="Copiar Resultado",
bd=2,
bg='#107db2',
fg='white',
font=('verdana', 12, 'bold'),
command=copiar_resultado
)
but_copiar.place(relx=0.35, rely=0.65, relwidth=0.3, relheight=0.08)
root.mainloop()