wxpython文件复制进度b

2024-05-19 01:36:09 发布

您现在位置:Python中文网/ 问答频道 /正文

朋友们,请帮助我如何更新wxwidgets python中的进度条,同时复制windows中的文件夹。我试了很多次。 我对python很在行,但不是很在行。只是想从控制台python编程切换到gui编程。请帮帮我。在

提前谢谢! 甘尼什R


Tags: 进度条文件夹windows编程朋友guiwxwidgets帮帮我
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

所有这些代码与原始copytree的不同之处在于在复制文件后调用指定的函数。所以在你的例子中,你可以让它更新对话框,或者更新成功复制的文件数,等等

使用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。在

相关问题 更多 >

    热门问题