【问题标题】:GUI freezing when shutil copy files当shutil复制文件时GUI冻结
【发布时间】:2013-09-23 17:40:15
【问题描述】:
def creabackuno():
  startbar()
  messagebox.showinfo( "Wait..","I am creating the backup, please wait...")
  try:
      copytree(path,r"backup\dirbackup1\.minecraft")
      messagebox.showinfo( "OK!","Backup (1) created!")
      stopbar()
  except OSError as exc:
      messagebox.showerror( "Nope!","There is already a backup to restore")
      stopbar()

我的进度条有问题:

startbar() 启动图形界面上的进度条,但是启动shutil(copytree(path,r"backup\dirbackup1.minecraft")) 界面冻结,进度条停止直到完成。 谢谢

我正在使用 python 3.3

对不起我的英语不好

【问题讨论】:

    标签: python tkinter progress-bar freeze ttk


    【解决方案1】:

    copytree 是一个同步函数,所以所有代码执行都会停止,直到它完成。虽然 tkinter 很遗憾没有线程安全,但我建议您将该命令放在另一个线程中:

    from thread import start_new_thread as snt
    #from _thread import start_new_thread as snt for python 3
    
    def copy(onError,onEnd):
        try: copytree(path,r"backup\dirbackup1\.minecraft")
        except: 
           onError()
           return
        onEnd()
    
    def onEnd():
        messagebox.showinfo( "OK!","Backup (1) created!")
        stopbar()
    
    def onError():
          messagebox.showerror( "Nope!","There is already a backup to restore")
          stopbar()
    
    #then call with
    
    snt(copy,(onError,onEnd))
    

    如果失败则执行 onError,成功则执行 onEnd。

    【讨论】:

    • 他说,他用的是python 3,而python3中的thread模块是_thread
    • 您有其他解决方案吗?这在我的代码中很难实现。
    【解决方案2】:

    进度条显示什么?如果您试图显示复制的文件的百分比,那么您必须首先获取文件的总长度/字节,然后使用复制的字节数定期更新。这将需要使用“之后”每隔几毫秒检查一次复制到文件的大小(我想我只是在这里猜测,但首先搜索,因为必须有人已经做过这样的事情。)这个是我找到的第一个链接https://mail.python.org/pipermail/tkinter-discuss/2010-December/002613.html 它可能比你想要的更多,但应该会有所帮助。

    【讨论】:

      【解决方案3】:

      在每个self.pgBar.step(x) 语句之后使用self.Frame.update_idletasks(),其中“x”代表进度条值增加的值

      【讨论】:

      • 进度条在不确定模式下运行,但是shutil开始复制文件时界面冻结
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2023-01-29
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多