[pygtk] Re: progressbar for file managing
Fabian Braennstroem
f.braennstroem at gmx.de
Wed Apr 18 05:15:33 WST 2007
Hi John,
John Dennis schrieb am 04/16/2007 06:05 PM:
> On Mon, 2007-04-16 at 19:55 +0000, Fabian Braennstroem wrote:
>> Hi,
>>
>> I would like to add a progressbar for file managing such as copying,
>> deleting, moving. Unfortunately I don't understand the handling of
>> the progressbar...
>> As a first try I would like to use the 'pulse' function for the
>> progressbar, i.e. as long as I copying files I would like to 'pulse'
>> the bar. Below you can see a small example; I get the progressbar
>> moving, but not during the copying process :-(
>>
>> #!/usr/bin/env python
>>
>>
>> import pygtk
>> pygtk.require('2.0')
>> import gtk
>> import shutil
>> from time import sleep
>>
>>
>> file1="/home/fab/.bashrc"
>>
>> class Buttons:
>> def callback(self, widget, data=None):
>> i=0
>> n=100
>>
>> self.progress.pulse()
>> self.progress.set_text("Calculating....")
>> self.progress.grab_add()
>>
>> shutil.copy(file1,"progressbar_copy")
>> while i < n:
>> sleep(0.015)
>> # self.progress.set_fraction(i/(n - 1.0))
>> self.progress.pulse()
>> i += 1
>>
>> while gtk.events_pending():
>> gtk.main_iteration_do(False)
>>
>> self.progress.set_fraction(0.0)
>> self.progress.set_text("")
>> self.progress.grab_remove()
>>
>> def __init__(self):
>> self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
>>
>> self.window.connect("destroy", lambda wid: gtk.main_quit())
>> self.window.connect("delete_event", lambda
>> a1,a2:gtk.main_quit())
>> self.window.set_border_width(10)
>> box = gtk.HBox()
>>
>> self.progress= gtk.ProgressBar()
>> button = gtk.Button("Button")
>> button.connect("clicked", self.callback, "cool button")
>>
>> button.show()
>> self.progress.show()
>> box.add(self.progress)
>> box.add(button)
>> box.show()
>>
>>
>> self.window.add(box)
>> self.window.show()
>>
>> def main():
>> gtk.main()
>> return 0
>>
>> if __name__ == "__main__":
>> Buttons()
>> main()
>>
>>
>> A next step would be, to display the remaing bytes and percentage of
>> the copied file until completion (like the midnight commander is
>> doing). How can I achive this? The progressbar has to know the size
>> and the actual copied/moved/deleted number of bytes... Does anyone
>> has an idea? Would be nice ...
>>
>> I am doing the file managing with 'shutil' or 'os' modules.
>
> Your basic problem is you're trying to code what is essentially a
> parallel operation in a synchronous manner. To move the progress bar you
> have to call the functions to move it (e.g. pulse or set_fraction), but
> you can't do that if you're waiting on a synchronous operation to
> complete (or worse calling sleep). You have a few choices, either do the
> copy yourself in a loop where you copy chunks of data and update the
> progress bar, find an API which does the copy in a thread and provide
> progress callbacks (sorry I'm not aware of such an API), or do the
> threading yourself. FWIW, here is a code snippet of mine, it installs a
> timeout function to pulse the progress bar every 1/10 sec until the
> operation completes. The trick here is that the operation is being
> handled in another thread so it proceeds independently of the the GUI
> thread.
>
> def progress_pulse(self):
> if self.load_in_progress:
> self.statusbar.progress.pulse()
> return True # call again
> else:
> return False # do not call again
>
> def on_load_data(self, list_view, alert_data, state, errno, strerror):
> if state == 'start':
> self.load_in_progress = True
> self.statusbar.progress.pulse()
> gobject.timeout_add(100, self.progress_pulse)
>
Thanks for your advice! I tried to adjust your lines a bit, but I
was not successful:
def progress_pulse(self):
if self.load_in_progress:
self.progress.pulse()
return True # call again
else:
return False # do not call again
def on_load_data(self , state ):
if state == 'start':
self.load_in_progress = True
self.progress.pulse()
gobject.timeout_add(1, self.progress_pulse)
if state == 'stop':
self.load_in_progress = False
def callback(self, widget, data=None):
i=0
n=100
self.on_load_data('start')
shutil.copytree(file1,"/home/fab/test_copy2")
self.on_load_data('stop')
The progressbar starts to run, if the directory already exists and
'shutil' stops with an error message; if the copying works than
there is no progressbar movement... strange. Do you know, what I am
doing wrong?
Greetings!
Fabian
>> Greetings!
>> Fabian
>>
>> _______________________________________________
>> pygtk mailing list pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
More information about the pygtk
mailing list