0

Long ago i watched a tutorial on how to encrypt files(of any kind) with a key/password The original code just makes the process in the terminal, but i wanted to make it into an application using tkinter as my GUI, i've come to a problem my small brain can't solve

The original video: https://www.youtube.com/watch?v=HHlInKhVz3s

This is the error i get: TypeError: Encrypt() missing 2 required positional arguments: 'WhichFile' and 'KeyInput'

This is my code:

from tkinter.filedialog import askopenfilename
import time


root = Tk()
root.title=("Tkinter Calculator")
root.geometry("500x500")



#title
WindowTitle = Label(root, text="Choose Action", font=("Arial", 15))
WindowTitle.place(x=250, y=10,anchor="center")

### The functions




#Encrypt
def Encrypt(WhichFile, KeyInput):
    file = open(WhichFile, "rb")
    data = file.read()
    file.close()
    
    data = bytearray(data)
    for index, value in enumerate(data):
        data[index] = value ^ KeyInput
        
    
    file = open("CC-" + WhichFile, "wb")
    file.write(data)
    file.close()
    




#Decrypt
def Decrypt(WhichFile, KeyInput):
    file = open(WhichFile, "rb")
    data = file.read()
    file.close()
    
    data = bytearray(data)
    for index, value in enumerate(data):
        data[index] = value ^ KeyInput
        
    
    file = open(WhichFile, "wb")
    file.write(data)
    file.close()
    







#Step1 - Write the name of the file(Needs to be in the same folder(Also include ext.))
WhichFile = Entry(root, width = 20)
WhichFile.place(x=100, y=150)
WhichFile.insert(0, "Enter File name with extension")

#Step2 - Ask for a key/password
KeyInput = Entry(root, width = 20)
KeyInput.place(x=100, y=250)
KeyInput.insert(0, "Enter a key: ")

#Button for encrypt
Encryptbtn = Button(root, text="Encrypt", highlightbackground='#3E4149', command=Encrypt)
Encryptbtn.place(x=100, y=350)




#Button for decrypt
Decryptbtn = Button(root, text="Decrypt", highlightbackground='#3E4149', command=Decrypt)
Decryptbtn.place(x=200, y=350)



root.mainloop()
X man
  • 1
  • 1
  • 1
    You have `command=Encrypt`. That means `tkinter` will call `Encrypt()` with no arguments but your `Encrypt` function needs 2 arguments `WhichFile`, and `KeyInput`. – TheLizzard Aug 12 '21 at 18:53

1 Answers1

0

So the error occurs in this line:

Encryptbtn = Button(root, text="Encrypt", highlightbackground='#3E4149', command=Encrypt)

Passing a function with arguments to Button's "command"

You have to pass the arguments to the function Encrypt() which demands args "WhichFile" and "KeyInput". You can pass arguments in Button declaration by using lambda keyword:

Button(root, text="Encrypt", highlightbackground='#3E4149', command=lambda:Encrypt(file,input))

It will take the values of "file" and "input" just when you click on the button, remember it cause sometime it is not that what you actually want (e.g. Python Tkinter button callback). If you want the arguments to be "remembered" as they was at moment of creating button, use currying (What is 'Currying'? , More about "lambda").


So, for first you have to pass arguments to that function. As I can see, you slightly don't understand how it works, cause you're trying to use the arguments in declaration of a funtion like they were a global variables of something (def Encrypt(WhichFile, KeyInput) and then as an "assignment" WhichFile = Entry(...)) but it doesn't work like that, arguments that are passed to function are specified in function's call, e.g. foo(argument1,argument2), not at the moment of defining it.

Getting text from Entry :

You have to know, that WhichFile = Entry(root, width = 20) is not assigning the value of Entry to WhichFile variable, but the Entry itself (check it by using print(WhichFile)). I propose changing the name of that variable for e.g. "user_input_file" or sth. If you want to get the text typed in the Entry use user_input_file.get(). Excatly the same thing with KeyInput Entry.

Next, you have to create a variable that will point to that value (python varaibles are pointers), and assign it in that function in a way I mentioned before (with that lambda:). Just write it as a global variable, for example:

WhichFile = user_input_file.get()
KeyInput = user_input_key.get()

Of course after declaring user_input_file and user_input_key.

I think that'll solve ur problem, feel free to ask 'bout more.

blackcaer
  • 23
  • 5