【问题标题】:wxpython filecopy progress barwxpython 文件复制进度条
【发布时间】:2012-04-03 17:08:47
【问题描述】:

朋友,请帮助我如何在 Windows 中复制文件夹时更新 WX Widgets python 中的进度条。我尝试了很多搜索。 我对 python 很感兴趣,但不是那么专家。只是想从控制台 python 编程切换到 gui 编程。请帮助我。

提前致谢! 加内什 R

【问题讨论】:

    标签: python wxpython wxwidgets file-copying


    【解决方案1】:

    使用ProgressDialog's Update 函数,如果您只需要向用户显示正在发生的事情,请使用UpdatePulse。

    pulse_dlg = wx.ProgressDialog(title="Dialog Title", message="Dialog Message", maximum=100)
    # Some stuff happens
    for i in range(10):
        wx.MilliSleep(250)
        pulse_dlg.Update(10*i)
    

    您还可以允许用户中止操作,请查看主题上的Mike Driscoll's excellent tutorial。

    【讨论】:

    • 谢谢你.. 但有一个问题.. 当我发布我的 copytree(source, destination) 语法代替“# Some stuff occur”时,进度条在完成文件夹及其副本的副本后开始内容... 请您解释一下如何整合 copytree(source, destination) 和进度条... 即复制文件夹及其内容,同时进度条也应该显示进度。
    • 如果你使用shutil.copytree,看起来你可以指定一个复制函数(它默认为copy2)。因此,一种方法可能是使用回调指定您自己的复制函数,该回调报告何时复制文件、复制的字节数等。或者使用threads 在一个线程中复制并监视目标中的# files/bytes .
    • 您能否在复制文件夹及其内容的同时使用更新进度条的 copy2 回调函数写下编码...?我不是python专家..
    • 更正 - 看起来 Python 3 允许您指定复制函数,但 Python 2.7 不允许。所以最简单的事情可能是编写一个稍微修改过的 copytree (source) 让它在文件被复制时报告回来。我将在下面发布一个演示。
    【解决方案2】:

    您可以编写自己的 copytree 稍加修改的版本,这会有所帮助:

    import shutil
    import os.path
    import os
    
    def file_copied():
        print "File copied!"
    
    # Usage example:  copytree(src, dst, cb=file_copied)
    def copytree(src, dst, symlinks=False, ignore=None, cb=None):
        """Recursively copy a directory tree using copy2().
    
        The destination directory must not already exist.
        If exception(s) occur, an Error is raised with a list of reasons.
    
        If the optional symlinks flag is true, symbolic links in the
        source tree result in symbolic links in the destination tree; if
        it is false, the contents of the files pointed to by symbolic
        links are copied.
    
        The optional ignore argument is a callable. If given, it
        is called with the `src` parameter, which is the directory
        being visited by copytree(), and `names` which is the list of
        `src` contents, as returned by os.listdir():
    
            callable(src, names) -> ignored_names
    
        Since copytree() is called recursively, the callable will be
        called once for each directory that is copied. It returns a
        list of names relative to the `src` directory that should
        not be copied.
    
        XXX Consider this example code rather than the ultimate tool.
    
        """
        names = os.listdir(src)
        if ignore is not None:
            ignored_names = ignore(src, names)
        else:
            ignored_names = set()
    
        os.makedirs(dst)
        errors = []
        for name in names:
            if name in ignored_names:
                continue
            srcname = os.path.join(src, name)
            dstname = os.path.join(dst, name)
            try:
                if symlinks and os.path.islink(srcname):
                    linkto = os.readlink(srcname)
                    os.symlink(linkto, dstname)
                elif os.path.isdir(srcname):
                    copytree(srcname, dstname, symlinks, ignore)
                else:
                    # Will raise a SpecialFileError for unsupported file types
                    shutil.copy2(srcname, dstname)
                    if cb is not None:
                        cb()
            # catch the Error from the recursive copytree so that we can
            # continue with other files
            except Error, err:
                errors.extend(err.args[0])
            except EnvironmentError, why:
                errors.append((srcname, dstname, str(why)))
        try:
            shutil.copystat(src, dst)
        except OSError, why:
            if WindowsError is not None and isinstance(why, WindowsError):
                # Copying file access times may fail on Windows
                pass
            else:
                errors.extend((src, dst, str(why)))
        if errors:
            raise Error, errors
    

    所有这些代码与原始复制树的不同之处在于它在复制文件后调用指定的函数。因此,在您的情况下,您可以使用 Update() 对话框代替 file_copied(),或更新成功复制的文件数等。

    【讨论】:

    • 感谢您的帮助.. 当我将您的代码插入我的 python 2.7 时,我收到以下错误 Traceback(最近一次调用最后):文件“C:\Python27\tr.py”,行131,在 createctrl self.copytree(source, destination) File "C:\Python27\tr.py",第 82 行,在 copytree names = os.listdir(src) TypeError: coercing to Unicode: need string or buffer, main found我在这里附上我的python脚本..我不知道如何在这里粘贴冗长的编码,当我这样做时,它会在此处显示“-4521个字符”..请下载并添加进度条功能rapidshare.com/files/3443229017/tr.py
    • 看起来你正在与一个缩进发生冲突,检查以确保你只使用制表符或空格字符而不是两者,并确保主类的方法都缩进相同等级。因为您使 copytree 成为一种方法,所以您需要将其声明更改为例如def copytree(self, src, dst, symlinks=False, ignore=None, cb=None)。我还建议先看一个很好的教程,Learn Python The Hard Way 是一个很好的教程。
    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 2011-08-28
    • 2010-11-20
    • 2018-05-04
    • 2012-12-05
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多