473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem understanding pygtk

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.FALS E,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.TR UE,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.T RUE,0)

self.label9 = gtk.Label(" ")
self.hbox0.pack_start(self.label9,gtk.FALSE,gtk.TR UE,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.TRU E,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.T RUE,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.T RUE,0)

self.label1 = gtk.Label(" ")
self.hbox1.pack_start(self.label1,gtk.FALSE,gtk.TR UE,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.TR UE,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.F ALSE,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.T RUE,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.TR UE,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()

--
Samuele Giovanni Tonon <to***@students.cs.unibo.it>
5% fats, 2% cerebral activities
Jul 18 '05 #1
2 3323
In article <sl******************@cantina.students.cs.unibo.it >, Samuele
Giovanni Tonon wrote:
basically i need to make pulse the progressbar while i'm copying files
and write filenames copied on the ListStore on to the window.


You need to call gtk.main_iteration() to "wake up" the event loop in between
copies. See

http://www.async.com.br/faq/pygtk/in...=faq23.020.htp

See also

http://www.async.com.br/faq/pygtk/in...=faq20.009.htp

Dave Cook
Jul 18 '05 #2
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.
--
I quit drinking and smoking once. Very boring 15 minutes.

|\ | |HomePage : http://nem01.altervista.org
| \|emesis |XPN (my nr): http://xpn.altervista.org

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fulko van Westrenen | last post by:
Hello, I use pyGTK in a program with ToggleButton and Pixmap. The problem I try top solve is changing the pixmap for the two states of the button. Can it be done? (should be possible), and...
4
by: j_mckitrick | last post by:
Hi all. Here is a tiny container for one of each combo box, along with the glade file. Just 2 widgets, so hopefully not too large. How the heck do I get the selection from the ComboBox, as...
6
by: Mark Mitchell | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I have a program written in python that tells me it requires PyGTK, so I DLed, ./configure, make, make install and the process seems to go ok. Some...
2
by: Friedrich Dimmling | last post by:
Hi, I would like to use pango.layout to display text in a pygtk2 project. However I could not manage to get the pango.layout object incorporated in my toplevel gtk.Window and have it displayed....
1
by: Thomas Bartkus | last post by:
I am experimenting (flailing around?) with glade and python. Both under MS Windows and Linux. I understand why I want to "import gtk" It gives me access to the critical gui program loop...
25
by: TPJ | last post by:
GUI's etc: PyGtk on Windows "(...) So if someone develops mainly for X and just wants to make sure that it is not impossible to run on Windows, you can use PyGTK. (...)", July 2nd, 1999 pyGTK...
14
by: Rod W | last post by:
I'm just starting out on Python but my primary goal is to provide applications with some user interface (GUI). Can someone point me to a good comparison of whether I should use wxPython (with...
1
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.0')
0
by: He Jibo | last post by:
Hi, Everyone, Could someone help me how to install pygtk? I get some problems with it. Here is the error I get while install pygtk: https://netfiles.uiuc.edu/jibohe2/error.GIF?uniq=-k6678k ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.