2012-04-03 66 views
2

朋友,请好心帮助我如何在Windows中复制文件夹时更新WX Widgets python中的进度条。我尝试了很多。 即时通讯python,但不是那么专业。只是想从控制台python编程切换到gui编程。请帮助我。wxpython filecopy进度条

在此先感谢! Ganesh R

回答

2

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

+0

谢谢你..但一个问题..当我发布我的在“#有些东西发生”的地方使用copytree(source,destination)语法,在完成文件夹及其内容的副本后,进度条会启动......您是否会解释如何将copytree(源,目标)和进度条即复制文件夹及其内容,同时进度条也应显示进度。 – 2012-04-04 03:32:12

+0

如果您使用[shutil.copytree](http://docs.python.org/library/shutil.html#module-shutil),它看起来像您可以指定一个复制功能(它默认为copy2)。所以一种方法可能是使用回调来指定自己的复制函数,该函数会报告文件被复制的时间,复制的字节数等。或者使用[threads](http://docs.python.org/library/) threading.html#模块线程)复制到一个线程中并监视目标中的#个文件/字节。 – ChrisC 2012-04-04 12:19:50

+0

可以ü用copy2回调函数记录编码,它更新进度条,同时复制文件夹及其内容..?我不是Python的专家.. – 2012-04-06 08:51:33

0

你可以写的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是做出特定功能的调用后,副本文件。所以在你的情况代替file_copied()你可以让它更新()对话框,或更新成功复制的文件数的计数等。

+0

thnk u for your help ..当我将你的代码插入到我的python 2.7中时我得到了以下错误 回溯(最近调用最后一次): createctrl中的文件“C:\ Python27 \ tr.py”,第131行 self.copytree(来源,目的地) 文件“C:\ Python27 \ tr.py”,第82行,在copyrree names = os.listdir(src) TypeError:强制为Unicode:需要字符串或缓冲区,主要找到 即时连接我的python脚本在这里..我不知道如何粘贴一个冗长的编码在这里,当我这样做时显示“-4521字符”在这里..请下载并添加进度条func https:// rapidshare。 com/files/3443229017/tr.py – 2012-04-07 03:04:02

+0

hello sir ...好心帮助我。 – 2012-04-09 13:49:11

+0

看起来你正与一个缩进行为发生冲突,请检查以确保只使用制表符或空格字符,但不能同时使用,并确保主类的方法都缩进到同一级别。由于您为copytree创建了一种方法,因此您需要将其声明更改为'def copytree(self,src,dst,symlinks = False,ignore = None,cb = None)'。我还建议看看一个好的教程来开始,[Learn Python The Hard Way](http://learnpythonthehardway.org/)是一个很好的教程。 – ChrisC 2012-04-09 15:01:52