0

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.

2
  • 1
    Yes you can, but you need to pass those required variables to the function via argumments. Commented Feb 17, 2023 at 6:07
  • 1
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the CC BY-SA 4.0 license, for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: How does deleting work? Commented Apr 25, 2023 at 13:21

1 Answer 1

1

Yes, you can move get_entry() out of convertFiles(), but you need to pass those required variables to the function via arguments:

def get_entry(win, passp, pass_files, decrypted_data):
    if passp:
        for file in pass_files:
            command1 = ["gpg", "-d", "--quiet", "--yes", "--pinentry-mode=loopback", f"--passphrase={passp}", f'{file}.gpg']
            out = subprocess.check_output(command1, universal_newlines=False)
            decrypted_data.append(out)
        print('Asymmetric decryption is complete.')
        win.destroy()


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)?') == "yes":
            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)

            add_button = tk.Button(newWindow, text='Enter',
                                   command=lambda: get_entry(newWindow, passp_entry.get(), pass_files, decrypted_data))
            add_button.place(x=0, y=60)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.