problem understanding pygtk

  • Thread starter Samuele Giovanni Tonon
  • Start date
S

Samuele Giovanni Tonon

hi,
i'm trying to develop a trivial application which random copy files
from a directory to another one.

i made it using pygtk for the graphical interface, however i find
some problem with progressbar and ListStore:

basically i need to make pulse the progressbar while i'm copying files
and write filenames copied on the ListStore on to the window.

However while copying the window is frozen, even if files are copied
one by one and when the copy process finish the window is updated
suddenly.
This behaviour is well shown if i try to copy files to slow devices
such as usb disk.

Looking to example in pygtk i though one way to solve this problem
was using gtk.timeouts or using thread , but none of them seems
to work.

Any hints ? The code is attached here, hoping someone can help me

Thanks
Samuele

----
#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import os
import time
import sys
import random
import shutil

def progress_timeout(pbobj):
if pbobj.transfermode == 1:
pbobj.progbar.pulse()
return gtk.TRUE


def copy_file(pbobj):
if pbobj.copying == 0 and pbobj.transfermode == 1:
pbobj.copying = 1
for f in pbobj.cplist:
shutil.copy2(f,pbobj.dstdir)
pbobj.model.append([f])
pbobj.cplist=[]
pbobj.statusbar.push(pbobj.statbarCID, "Transfer Done")
pbobj.transfermode = 0
pbobj.copying = 0
return gtk.TRUE


class RanFiles:
def listfiles(self):
if len(self.model) >= 1:
self.model.clear()
self.fileslist = []
for root, dirs, files in os.walk(self.pathname):
for f in files:
self.fileslist.append(root + '/' + f)

def enter_callback(self, widget, entry):
self.dstdir = self.txtentry.get_text()

def src_callback(self, widget, entry):
self.pathname = self.txtentry0.get_text()

def delete_event(self, widget, event, data=None):
return gtk.FALSE

def destroy(self, widget, data=None):
self.statusbar.pop(self.statbarCID)
gtk.timeout_remove(self.timer)
gtk.timeout_remove(self.timer1)
self.timer = 0
self.timer1 = 0
gtk.main_quit()

def refresh_menu(self, widget, data=None):
self.statusbar.pop(self.statbarCID)
self.listfiles()

def random_menu(self, widget, data=None):
self.statusbar.pop(self.statbarCID)
self.defsize = self.spinner.get_value_as_int()
maxsize= self.defsize * 1024 * 1024
self.cplist=[]
complete = 0
size = 0
while complete == 0:
file = self.fileslist[random.randint(0,len(self.fileslist)-1)]
fsize = int(os.stat(file)[6])
if (size + fsize) < maxsize:
self.cplist.append(file)
size = size + fsize
else:
complete = 1
self.transfer_menu(widget)

def transfer_menu(self, widget, data=None):
if self.dstdir == "":
self.dstdir_menu(widget)

self.statusbar.pop(self.statbarCID)
self.model.clear()
self.transfermode = 1

def dstdir_menu(self, widget, data=None):
self.statusbar.pop(self.statbarCID)
dir='/'
dlg = gtk.FileSelection(title = 'Select destination directory...')
dlg.file_list.set_sensitive(0)
dlg.set_filename(dir)
response = dlg.run()
if response == gtk.RESPONSE_OK:
self.dstdir = dlg.get_filename()
else:
self.dstdir = '/'
self.txtentry.set_text(self.dstdir)
dlg.destroy()


def browse_menu(self, widget, data=None):
self.statusbar.pop(self.statbarCID)
dir=self.pathname+'/'
if(dir=='/'):
dir = "~/"
dlg = gtk.FileSelection(title = 'Select directory...')
dlg.file_list.set_sensitive(0)
dlg.set_filename(dir)
response = dlg.run()
if response == gtk.RESPONSE_OK:
self.pathname = dlg.get_filename()
self.listfiles()
else:
self.pathname = '/'
self.txtentry0.set_text(self.pathname)
dlg.destroy()

def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Random Files")
self.window.set_geometry_hints(min_width=360, min_height=200)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.pathname = "~/"
self.transfermode = 0
self.dstdir = ""
self.defsize = 128
self.copying = 0

self.vbox1 = gtk.VBox(gtk.FALSE,0)
self.window.add(self.vbox1)

self.hsep = gtk.HSeparator()
self.vbox1.pack_start(self.hsep,gtk.FALSE,gtk.FALSE,0)


self.hbox0 = gtk.HBox(gtk.FALSE,0)
self.vbox1.pack_start(self.hbox0,gtk.FALSE, gtk.FALSE,0)

self.label0 = gtk.Label("Src Dir: ")
self.hbox0.pack_start(self.label0,gtk.FALSE,gtk.TRUE,0)

self.txtentry0 = gtk.Entry(max=0)
self.txtentry0.set_text(self.pathname)
self.txtentry0.connect("activate", self.src_callback, self.txtentry0)
self.hbox0.pack_start(self.txtentry0,gtk.TRUE,gtk.TRUE,0)

self.button1 = gtk.Button(None,"Browse")
self.button1.connect_object("clicked", self.browse_menu, None)
self.hbox0.pack_start(self.button1,gtk.FALSE,gtk.TRUE,0)

self.label9 = gtk.Label(" ")
self.hbox0.pack_start(self.label9,gtk.FALSE,gtk.TRUE,0)

self.hbox1 = gtk.HBox(gtk.FALSE,0)
self.vbox1.pack_start(self.hbox1,gtk.FALSE, gtk.FALSE,0)

self.label = gtk.Label("Dst Dir: ")
self.hbox1.pack_start(self.label,gtk.FALSE,gtk.TRUE,0)

self.txtentry = gtk.Entry(max=0)
self.txtentry.set_text(self.dstdir)
self.txtentry.connect("activate", self.enter_callback, self.txtentry)
self.hbox1.pack_start(self.txtentry,gtk.TRUE,gtk.TRUE,0)

self.button5 = gtk.Button(None,"Browse")
self.button5.connect_object("clicked", self.dstdir_menu, None)
self.hbox1.pack_start(self.button5,gtk.FALSE,gtk.TRUE,0)

self.label1 = gtk.Label(" ")
self.hbox1.pack_start(self.label1,gtk.FALSE,gtk.TRUE,0)

self.hbox2 = gtk.HBox(gtk.FALSE,0)
self.vbox1.pack_start(self.hbox2,gtk.FALSE, gtk.FALSE,0)

self.label2 = gtk.Label("Size (MB): ")
self.hbox2.pack_start(self.label2,gtk.FALSE,gtk.TRUE,0)

self.adj = gtk.Adjustment(self.defsize, 1.0, 512.0, 1.0, 10.0, 0.0)
self.spinner = gtk.SpinButton(self.adj, 0, 0)
self.spinner.set_wrap(gtk.TRUE)
self.hbox2.pack_start(self.spinner, gtk.FALSE, gtk.TRUE, 0)

self.toolbar = gtk.Toolbar()
self.toolbar.set_style(gtk.TOOLBAR_BOTH)
self.vbox1.pack_start(self.toolbar,gtk.FALSE,gtk.FALSE,0)

self.toolbutton2 = gtk.ToolButton(None,"Transfer")
self.toolbutton2.connect_object("clicked", self.transfer_menu, None)
self.toolbar.add(self.toolbutton2)

self.toolbutton3 = gtk.ToolButton(None,"Random")
self.toolbutton3.connect_object("clicked", self.random_menu, None)
self.toolbar.add(self.toolbutton3)

self.toolbutton4 = gtk.ToolButton(None,"Quit")
self.toolbutton4.connect_object("clicked", self.destroy, None)
self.toolbar.add(self.toolbutton4)

self.scwindow = gtk.ScrolledWindow()
self.scwindow.set_shadow_type(gtk.SHADOW_IN)
self.vbox1.pack_start(self.scwindow,gtk.TRUE,gtk.TRUE,0)

self.model = gtk.ListStore(gobject.TYPE_STRING)
self.listfiles()
self.treeview = gtk.TreeView(self.model)
self.column = gtk.TreeViewColumn("Folder", gtk.CellRendererText(), text=0)
self.treeview.append_column(self.column)
self.treeview.set_headers_visible(gtk.FALSE)
self.scwindow.add(self.treeview)

self.hbox = gtk.HBox(gtk.TRUE,0)
self.vbox1.pack_start(self.hbox,gtk.FALSE, gtk.FALSE,0)

self.progbar = gtk.ProgressBar()
self.progbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
self.progbar.set_pulse_step(0.1)
self.hbox.pack_start(self.progbar,gtk.FALSE,gtk.TRUE,0)
self.timer = gtk.timeout_add (100, progress_timeout, self)
self.timer1 = gtk.timeout_add (100, copy_file, self)

self.statusbar = gtk.Statusbar()
self.statbarCID = self.statusbar.get_context_id('my stat bar')
self.hbox.pack_start(self.statusbar,gtk.FALSE,gtk.TRUE,0)

self.vbox1.show()
self.hsep.show()
self.toolbar.show()
self.toolbutton3.show()
self.toolbutton4.show()
self.hbox0.show()
self.label0.show()
self.txtentry0.show()
self.button1.show()
self.hbox1.show()
self.label.show()
self.label9.show()
self.txtentry.show()
self.button5.show()
self.label1.show()
self.hbox2.show()
self.label2.show()
self.spinner.show()
self.scwindow.show()
self.treeview.show()
self.hbox.show()
self.progbar.show()
self.statusbar.show()
self.window.show()


def main(self):
gtk.main()

if __name__ == "__main__":
RM = RanFiles()
RM.main()
 
N

Nemesis

Mentre io pensavo ad una intro simpatica "Samuele Giovanni Tonon"
scriveva:
hi,
i'm trying to develop a trivial application which random copy files
from a directory to another one.

i made it using pygtk for the graphical interface, however i find
some problem with progressbar and ListStore:

basically i need to make pulse the progressbar while i'm copying files
and write filenames copied on the ListStore on to the window.

However while copying the window is frozen, even if files are copied
one by one and when the copy process finish the window is updated
suddenly.
This behaviour is well shown if i try to copy files to slow devices
such as usb disk.

you should use:

while gtk.events_pending():
gtk.mainiteration(gtk.FALSE)

in the loop, it requests a refresh of the gui.
So if you use a loop to iterate over a list of files insert that two
lines in the loop, and your gui will be refreshed every iteration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top