The intention is to have a user choose a file via
select_file_en
which will then be encrypted via
encrypt
I want the program to save the filename of the chosen file in a variable, which can be accessed by the encryption part. I'm a newbie so please bare with me if this is easy to fix. Code can be found at https://github.com/KDropZ/NDA/blob/main/main.py and is by far not final at all. When I run the encrypt part I get the following Error, so my way to "call" the variable seems to be wrong I guess?
Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args) TypeError: encrypt() missing 1 required positional argument: 'filename'
Additional info: Python 3.8.10, Tkinter 8.6, Ubuntu OS
Code example
import tkinter as tk
from tkinter import ttk
import tkinter.font as font
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo
import os
def select_file_en():
filetypes = (
('All files', '*.*'),
)
filename = fd.askopenfilename(
title='Choose a file to encrypt',
initialdir='/',
filetypes=filetypes)
showinfo(
title='Selected File',
message=filename
)
def encrypt(filename):
to_encrypt = open(filename, "rb").read()
size = len(to_encrypt)
key = os.urandom(size)
with open(filename + ".key", "wb") as key_out:
key_out.write(key)
encrypted = bytes(a^b for (a,b) in zip(filename, key))
with open(filename, "wb") as encrypted_out:
encrypted_out.write(encrypted)