So I was developing a GUI with tkinter and I have realised that I have so many nested functions in my code that I think it is not a good practice. However, I think my code will not work without using them.
My code:
def convertFiles():
files = []
cname = my_lb.curselection()
for i in cname:
op = my_lb.get(i)
files.append(op)
if files == []:
messagebox.showinfo('', 'Please choose a file')
else:
pass_files = []
decrypted_data = []
if messagebox.askquestion('Convert', 'Are you sure to convert your file(s)?'):
kill_command = ["gpgconf", "--kill", "gpg-agent"]
kill_out = subprocess.check_output(kill_command, universal_newlines=False)
disable_button()
for val in files:
pass_files.append(f'{pwd}/.password-store/{val}')
newWindow = tk.Toplevel(root)
newWindow.title("Passphrase for Asymmetric Decryption")
newWindow.geometry("400x150")
newWindow.resizable(False,False)
tk.Label(newWindow,text ="Please put your passphrase for decryption.").place(x=0,y=0)
passp_entry = tk.Entry(newWindow)
passp_entry.place(x=0,y=30)
def get_entry():
passp = passp_entry.get()
if passp:
for x in range(0, len(pass_files)):
command1 = ["gpg", "-d", "--quiet", "--yes", "--pinentry-mode=loopback", f"--passphrase={passp}", f'{pass_files[x]}.gpg']
out = subprocess.check_output(command1, universal_newlines=False)
decrypted_data.append(out)
print('Asymmetric decryption is complete.')
newWindow.destroy()
add_button = tk.Button(newWindow, text='Enter', command=get_entry)
add_button.place(x=0, y=60)
I really just want to know if get_entry() can be put outside and be called inside of the convertFiles() function. I think that might not be possible because get_entry() needs variables that is inside the convertFiles(). I will be happy to hear your thoughts.