Python tkinter移除嵌套函数

xienkqul  于 2023-02-18  发布在  Python
关注(0)|答案(1)|浏览(132)

所以我用tkinter开发了一个图形用户界面,我意识到我的代码中有太多的嵌套函数,我认为这不是一个好的实践,但是,我认为我的代码不使用它们就不能工作。
我的代码:

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)

我真的只是想知道get_entry()是否可以放在convertFiles()函数的外部,并在convertFiles()函数内部调用。我认为这可能是不可能的,因为get_entry()需要convertFiles()内部的变量。我很高兴听到您的想法。

dgtucam1

dgtucam11#

可以,您可以将get_entry()移出convertFiles(),但需要通过参数将这些必需的变量传递给函数:

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)

相关问题