【问题标题】:Tkinter lag when file encrypting文件加密时 Tkinter 滞后
【发布时间】:2021-12-21 13:32:24
【问题描述】:

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

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

有没有什么办法让它不卡顿?

【问题讨论】:

  • 您的加密功能可能阻止了.mainloop,您可能想在另一个线程中进行加密,还应在问题中提供代码作为文本和minimal reproducible example
  • 您将进行 10k 次迭代,这将需要大量处理,并且在这个数量上可能不会获得太多收益。无论如何,密码学无论如何都需要一些时间。您应该在这里使用不同的进程或至少一个线程。

标签: python tkinter multiprocessing


【解决方案1】:

试试这个:

在函数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。

【讨论】:

  • 谢谢。请您看看here 我尝试了所有方法,但似乎没有用。这是我需要的最后帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
  • 2017-08-18
  • 1970-01-01
  • 2015-09-15
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多