Search code examples
pythontkinter

I can't get the input and output to work with my function in python (TKINTER)


I can't get my tkinter project to work. i haven't been succesful into getting the input from the entry label from a user to then that info get worked on through my function of encryptation, so it can print the output in the output label.

from tkinter import *

from cryptography.fernet import Fernet

raiz= Tk()


#def output():#
 #   texto.set(print(Encriptador))#

raiz.title("Codec")

texto= StringVar()

raiz.resizable(False, False)

#raiz.iconbitmap("logo.ico")#

#raiz.geometry("650x360")#

raiz.config(bg="dark green")

frame1= Frame()
frame1.pack()
frame1.config(bg="dark green")
frame1.config(width=650,height=400)

prompt= Label(text="Coloque el texto a encriptar", fg="black", font="Bold", bg="dark green")
prompt.place(x=230,  y=80)

def Encriptador():
    mensaje= cuadroinput.get

    key= Fernet.generate_key()

    cifrar= Fernet(key)

    mensaje_cifrado = cifrar.encrypt(mensaje.encode())

    cuadroutput.config(Text= mensaje_cifrado)




cuadroinput=Entry(frame1, width=40)
cuadroinput.place(x=200 , y=120)

cuadroutput=Entry(frame1, width=70,  textvariable=texto)
cuadroutput.place(x=100, y=300)

boton=Button(raiz,text="Encriptar", command=Encriptador)
boton.place(x=305, y=170)


raiz.mainloop()

Solution

  • The problem with your code is:

    def Encriptador():
        mensaje= cuadroinput.get # <------ here
    
        key= Fernet.generate_key()
    
        cifrar= Fernet(key)
    
        mensaje_cifrado = cifrar.encrypt(mensaje.encode())
    
        cuadroutput.config(Text= mensaje_cifrado)
    

    The .get that you are calling, is a function, and a function, in Python, must have (). The fix is simply changing mensaje= cuadroinput.get to mensaje= cuadroinput.get()

    Another thing you can do is give cuadroinput a textvariable, and create a StringVar().

    Here is an example, using your code:

    invar = StringVar()
    
    def Encriptador():
        mensaje= invar.get()
    
        key= Fernet.generate_key()
    
        cifrar= Fernet(key)
    
        mensaje_cifrado = cifrar.encrypt(mensaje.encode())
    
        cuadroutput.config(Text= mensaje_cifrado)
    
    cuadroinput=Entry(frame1, width=40, textvariable = invar)