文件加密时 Tkinter 滞后

Tkinter lag when file encrypting

我做了一个简单的加密应用程序,这是代码 - https://codeshare.io/ZJ7qJn

但是当我按下加密时,我的 tkinter 应用程序滞后并显示“无响应”,因此我无法在 tkinter 应用程序中按下任何内容,但它确实完成了加密过程。

有什么方法可以让它没有延迟吗?

试试这个:

在函数 encfile 中将对 fiencdone() 的调用替换为:

    root.event_generate("<<encryption_done>>")

在函数encfile的定义之后添加如下新的函数定义:

def run_encfile():
    from threading import Thread

    root.bind('<<encryption_done>>', encryption_done)
    Thread(target=encfile).start()

def encryption_done(*args):
    fiencdone()

最后,更改第 75 行以调用 run_encfile 而不是 encfile:

file_enc_button = tk.Button(fibutton_frame, text='Encrypt',font='Raleway 15 bold', width=15,command=run_encfile, borderwidth=3)

这将 运行 在单独的线程中进行加密,但会调用 fiencdone 表示加密已在主线程中完成,这是必需的,因为它会更新 GUI。