473,835 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem understanding pygtk

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



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

def progress_timeou t(pbobj):
if pbobj.transferm ode == 1:
pbobj.progbar.p ulse()
return gtk.TRUE
def copy_file(pbobj ):
if pbobj.copying == 0 and pbobj.transferm ode == 1:
pbobj.copying = 1
for f in pbobj.cplist:
shutil.copy2(f, pbobj.dstdir)
pbobj.model.app end([f])
pbobj.statusbar .push(pbobj.sta tbarCID, "Transfer Done")
pbobj.transferm ode = 0
pbobj.copying = 0
return gtk.TRUE
class RanFiles:
def listfiles(self) :
if len(self.model) >= 1:
self.model.clea r()
self.fileslist = []
for root, dirs, files in os.walk(self.pa thname):
for f in files:
self.fileslist. append(root + '/' + f)

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

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

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

def destroy(self, widget, data=None):
self.statusbar. pop(self.statba rCID)
gtk.timeout_rem ove(self.timer)
gtk.timeout_rem ove(self.timer1 )
self.timer = 0
self.timer1 = 0

def refresh_menu(se lf, widget, data=None):
self.statusbar. pop(self.statba rCID)
self.listfiles( )

def random_menu(sel f, widget, data=None):
self.statusbar. pop(self.statba rCID)
self.defsize = self.spinner.ge t_value_as_int( )
maxsize= self.defsize * 1024 * 1024
complete = 0
size = 0
while complete == 0:
file = self.fileslist[random.randint( 0,len(self.file slist)-1)]
fsize = int(os.stat(fil e)[6])
if (size + fsize) < maxsize:
self.cplist.app end(file)
size = size + fsize
complete = 1
self.transfer_m enu(widget)

def transfer_menu(s elf, widget, data=None):
if self.dstdir == "":
self.dstdir_men u(widget)

self.statusbar. pop(self.statba rCID)
self.model.clea r()
self.transfermo de = 1

def dstdir_menu(sel f, widget, data=None):
self.statusbar. pop(self.statba rCID)
dlg = gtk.FileSelecti on(title = 'Select destination directory...')
dlg.file_list.s et_sensitive(0)
dlg.set_filenam e(dir)
response = dlg.run()
if response == gtk.RESPONSE_OK :
self.dstdir = dlg.get_filenam e()
self.dstdir = '/'
self.txtentry.s et_text(self.ds tdir)
def browse_menu(sel f, widget, data=None):
self.statusbar. pop(self.statba rCID)
dir=self.pathna me+'/'
dir = "~/"
dlg = gtk.FileSelecti on(title = 'Select directory...')
dlg.file_list.s et_sensitive(0)
dlg.set_filenam e(dir)
response = dlg.run()
if response == gtk.RESPONSE_OK :
self.pathname = dlg.get_filenam e()
self.listfiles( )
self.pathname = '/'
self.txtentry0. set_text(self.p athname)

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.con nect("delete_ev ent", self.delete_eve nt)
self.window.con nect("destroy", self.destroy)
self.pathname = "~/"
self.transfermo de = 0
self.dstdir = ""
self.defsize = 128
self.copying = 0

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

self.hsep = gtk.HSeparator( )
self.vbox1.pack _start(self.hse p,gtk.FALSE,gtk .FALSE,0)
self.hbox0 = gtk.HBox(gtk.FA LSE,0)
self.vbox1.pack _start(self.hbo x0,gtk.FALSE, gtk.FALSE,0)

self.label0 = gtk.Label("Src Dir: ")
self.hbox0.pack _start(self.lab el0,gtk.FALSE,g tk.TRUE,0)

self.txtentry0 = gtk.Entry(max=0 )
self.txtentry0. set_text(self.p athname)
self.txtentry0. connect("activa te", self.src_callba ck, self.txtentry0)
self.hbox0.pack _start(self.txt entry0,gtk.TRUE ,gtk.TRUE,0)

self.button1 = gtk.Button(None ,"Browse")
self.button1.co nnect_object("c licked", self.browse_men u, None)
self.hbox0.pack _start(self.but ton1,gtk.FALSE, gtk.TRUE,0)

self.label9 = gtk.Label(" ")
self.hbox0.pack _start(self.lab el9,gtk.FALSE,g tk.TRUE,0)

self.hbox1 = gtk.HBox(gtk.FA LSE,0)
self.vbox1.pack _start(self.hbo x1,gtk.FALSE, gtk.FALSE,0)

self.label = gtk.Label("Dst Dir: ")
self.hbox1.pack _start(self.lab el,gtk.FALSE,gt k.TRUE,0)

self.txtentry = gtk.Entry(max=0 )
self.txtentry.s et_text(self.ds tdir)
self.txtentry.c onnect("activat e", self.enter_call back, self.txtentry)
self.hbox1.pack _start(self.txt entry,gtk.TRUE, gtk.TRUE,0)

self.button5 = gtk.Button(None ,"Browse")
self.button5.co nnect_object("c licked", self.dstdir_men u, None)
self.hbox1.pack _start(self.but ton5,gtk.FALSE, gtk.TRUE,0)

self.label1 = gtk.Label(" ")
self.hbox1.pack _start(self.lab el1,gtk.FALSE,g tk.TRUE,0)

self.hbox2 = gtk.HBox(gtk.FA LSE,0)
self.vbox1.pack _start(self.hbo x2,gtk.FALSE, gtk.FALSE,0)

self.label2 = gtk.Label("Size (MB): ")
self.hbox2.pack _start(self.lab el2,gtk.FALSE,g tk.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.se t_wrap(gtk.TRUE )
self.hbox2.pack _start(self.spi nner, gtk.FALSE, gtk.TRUE, 0)

self.toolbar = gtk.Toolbar()
self.toolbar.se t_style(gtk.TOO LBAR_BOTH)
self.vbox1.pack _start(self.too lbar,gtk.FALSE, gtk.FALSE,0)

self.toolbutton 2 = gtk.ToolButton( None,"Transfer" )
self.toolbutton 2.connect_objec t("clicked", self.transfer_m enu, None)
self.toolbar.ad d(self.toolbutt on2)

self.toolbutton 3 = gtk.ToolButton( None,"Random")
self.toolbutton 3.connect_objec t("clicked", self.random_men u, None)
self.toolbar.ad d(self.toolbutt on3)

self.toolbutton 4 = gtk.ToolButton( None,"Quit")
self.toolbutton 4.connect_objec t("clicked", self.destroy, None)
self.toolbar.ad d(self.toolbutt on4)

self.scwindow = gtk.ScrolledWin dow()
self.scwindow.s et_shadow_type( gtk.SHADOW_IN)
self.vbox1.pack _start(self.scw indow,gtk.TRUE, gtk.TRUE,0)

self.model = gtk.ListStore(g object.TYPE_STR ING)
self.listfiles( )
self.treeview = gtk.TreeView(se lf.model)
self.column = gtk.TreeViewCol umn("Folder", gtk.CellRendere rText(), text=0)
self.treeview.a ppend_column(se lf.column)
self.treeview.s et_headers_visi ble(gtk.FALSE)
self.scwindow.a dd(self.treevie w)

self.hbox = gtk.HBox(gtk.TR UE,0)
self.vbox1.pack _start(self.hbo x,gtk.FALSE, gtk.FALSE,0)

self.progbar = gtk.ProgressBar ()
self.progbar.se t_orientation(g tk.PROGRESS_LEF T_TO_RIGHT)
self.progbar.se t_pulse_step(0. 1)
self.hbox.pack_ start(self.prog bar,gtk.FALSE,g tk.TRUE,0)
self.timer = gtk.timeout_add (100, progress_timeou t, 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.stat usbar,gtk.FALSE ,gtk.TRUE,0)

self.vbox1.show ()
self.hsep.show( )
self.toolbar.sh ow()
self.toolbutton 3.show()
self.toolbutton 4.show()
self.hbox0.show ()
self.label0.sho w()
self.txtentry0. show()
self.button1.sh ow()
self.hbox1.show ()
self.label.show ()
self.label9.sho w()
self.txtentry.s how()
self.button5.sh ow()
self.label1.sho w()
self.hbox2.show ()
self.label2.sho w()
self.spinner.sh ow()
self.scwindow.s how()
self.treeview.s how()
self.hbox.show( )
self.progbar.sh ow()
self.statusbar. show()
self.window.sho w()
def main(self):

if __name__ == "__main__":
RM = RanFiles()

Samuele Giovanni Tonon <to***@students .cs.unibo.it>
5% fats, 2% cerebral activities
Jul 18 '05 #1
2 3360
In article <sl************ ******@cantina. students.cs.uni bo.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_iterat ion() to "wake up" the event loop in between
copies. See


See also


Dave Cook
Jul 18 '05 #2
Mentre io pensavo ad una intro simpatica "Samuele Giovanni Tonon"
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
This behaviour is well shown if i try to copy files to slow devices
such as usb disk.

you should use:

while gtk.events_pend ing():
gtk.mainiterati on(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

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 someone provide me with working code? The code I use now is (python-2.2, gtk-1.2): button = gtk.GtkToggleButton()
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 opposed to the ComboEntryBox? I'm pulling my hair out over this one! jonathon
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 errors but the process finishes so I assumed they weren't fatal errors. So now I attempt to run my python program and I get; $ gccp.py Traceback (most recent call last):
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. If anyone could help me either with a minimal example like 'Hello World' displayed in italics in an pango.layout or the URL to a tutorial where I can find the relevant infos (the PyGtk tutorial seems not to contain it, unless I'm really blind)
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 gtk.main() and main_quit() I am also very grateful for import gtk.glade This lets me open my xml format glade file
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 on Windows "(...) > can i use pyGTK under > Windows???
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 wxGlade I assume) or PyGTK (with Glade I assume)? I'd prefer open source (not necessarily GPL though) tools. Rod
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.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 I use python 2.4, I first installed glade-2.0.1-win32_with_gtk, and then pycairo-1.0.2-1.win32-py2.4, pygobject-2.12.3-1.win32-py2.4, and
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.