[pygtk] critique my GUI

Christopher Spears cspears2002 at yahoo.com
Tue Aug 8 06:51:03 WST 2006


I created a GUI in glade consisting of two text
entries.  The first text entry is labelled Base path,
which is initially set to ".", and the second is
labelled Glob pattern.  After typing a glob pattern
and hitting ENTER, the script examines the current
working directory and the path in Base path for files
that match the glob.  Right now, those files are
printed to the screen, but eventually, I want to print
them to the GUI.  The .glade and the .py files are
attached to this email.  I have also included the .py
file as inline text:  

#!/usr/bin/python

import glob, os, os.path, sys
try:
	import pygtk
	pygtk.require("2.0")
except:
	pass
	
try:
	import gtk
	import gtk.glade
except:
	sys.exit(1)
	
class findFileGUI:
	"""A GUI used to find files with glob"""
	
	def on_mainWindow_delete(self, widget, event):
		gtk.main_quit()
		
	def create_dict(self, path, globbed_dict, pattern):
		cwd = os.getcwd()
		os.chdir(path)
		matched_files = glob.glob(pattern)
		if matched_files != []:
			globbed_dict[path] = matched_files
		#Make sure the script returns to the user's original
directory
		os.chdir(cwd)
		return globbed_dict
		
	def print_dict(self, globbed_dict):
		if globbed_dict == {}:
			print "No files found.\n"
		else:
			paths = globbed_dict.keys()
			paths.sort()
			for p in paths:
				print p,": "
				file_list = globbed_dict[p]
				for f in file_list:
					print "\t",f

	def glob_files(self, widget, path_entry, glob_entry,
results_label):
		base_path = path_entry.get_text()
		abs_base = os.path.abspath(base_path)
		pattern = glob_entry.get_text()
		globbed_dict = {}
		#Check the root directory first
		globbed_dict = self.create_dict(abs_base,
globbed_dict, pattern)
		#Check other directories
		for root,dirs,files in os.walk(abs_base):
			for name in dirs:
				path = os.path.join(root, name)
				globbed_dict = self.create_dict(path,
globbed_dict, pattern)
		self.print_dict(globbed_dict)
		abs_base = " "
		path_entry.set_text('.')
				
	def __init__(self):
		self.gladefile = "find_files.glade"
		self.wTree = gtk.glade.XML(self.gladefile)
		
		path_entry = self.wTree.get_widget("path_entry")
		glob_entry = self.wTree.get_widget("glob_entry")
		results_label =
self.wTree.get_widget("results_label")
		
		dic = { "on_mainWindow_delete_event" :
self.on_mainWindow_delete, 
			"on_glob_entry_activate" : (self.glob_files,
path_entry, glob_entry, results_label) }
		self.wTree.signal_autoconnect(dic)
		
	
if __name__ == "__main__":
	fFG = findFileGUI()
	main = gtk.main()
	

	

	
-------------- next part --------------
A non-text attachment was scrubbed...
Name: find_files.glade
Type: application/x-glade
Size: 6299 bytes
Desc: 4005518452-find_files.glade
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20060807/c519f0a4/find_files.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: find_files_02.py
Type: text/x-python
Size: 2440 bytes
Desc: 3312650066-find_files_02.py
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20060807/c519f0a4/find_files_02.py


More information about the pygtk mailing list