2012-04-03 17 views
2

フレンドは、Windowsのフォルダをコピー中にWX Widgetsのpythonの進行状況バーを更新する方法を親切に教えてください。私はたくさんの検索を試みました。 Imはpythonにgudしていますが、あまり専門家ではありません。ちょうどコンソールPythonプログラミングからGUIプログラミングに切り替えることを考えました。親切に私を助けてください。wxpythonのファイルコピーの進行状況バー

ありがとうございます! 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

ありがとうございます。 "#Some stuff stuff"の代わりにcopytree(ソース、デスティネーション)の構文を使用すると、フォルダとその内容のコピーを完了した後にプログレスバーが始まります... copytree(ソース、デスティネーション)とプログレスバー...つまり、フォルダとその内容をコピーすると同時に、進捗バーに進行状況も表示されます。 –

+0

[shutil.copytree](http://docs.python.org/library/shutil.html#module-shutil)を使用している場合は、コピー機能を指定できるように見えます(デフォルトはcopy2)。そのためには、ファイルのコピー時、コピーされたバイト数などをレポートするコールバックを使用して、独自のコピー関数を指定する方法があります。または、[threads](http://docs.python.org/library/ threading.html#module-threading)を使用して1つのスレッドでコピーし、宛先の#ファイル/バイトを監視します。 – ChrisC

+0

フォルダとその内容をコピーしながら、プログレスバーを更新するcopy2コールバック関数を使ってコードを書き留めてもよろしいですか?私はPythonの専門家ではありません。 –

0

あなたがお手伝いしますcopytreeの独自の非常に-やや修正したバージョン書くことができます。すべてこのコードは、元copytreeがそれをコピーAの後に指定した関数への呼び出しを行うことであるとは異なってい

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 

をファイル。ですから、あなたの場合は、file_copied()の代わりにダイアログを更新()したり、正常にコピーされたファイルの数を更新したりすることができます。

+0

あなたの助けにthnk u ..私のPython 2.7にあなたのコードを挿入したときに次のエラーが発生しました トレースバック(最新の最後の呼び出し): ファイル "C:\ Python27 \ tr.py"、行131、createctrl self.copytree(source、destination) copytreeのファイル "C:\ Python27 \ tr.py"、行82、 名前= os.listdir(src) TypeError:Unicodeに強制する:必要な文字列またはバッファー、main found 私のpythonスクリプトをここに添付しています。ここで長いコードを貼り付ける方法はわかりません。ここに「-4521 characters」と表示されます。親切にダウンロードして、進行状況バーfuncもhttps:// rapidshareに追加してください。 com/files/3443229017/tr.py –

+0

hello sir ...親切に私を助けてください。 –

+0

あなたは1つのインデントのためにインデントされているように見えますが、タブやスペース文字のみを使用していることを確認してください。両方を同時に使用しないようにして、メインクラスのメソッドがすべて同じレベルにインデントされていることを確認してください。あなたはcopytreeにメソッドを宣言したので、その宣言を例えばに変更する必要があります。 'def copytree(self、src、dst、symlinks = False、ignore = None、cb = None)'です。私はまた、[チュートリアルを始めるには良いチュートリアルを見てみることをお勧めします。[Pythonの難しい方法](http://learnpythonthehardway.org/)は良いものです。 – ChrisC

関連する問題