Re: Unable to get Python/Gtk code to work...
- From: "Daniel B. Thurman" <dant cdkkt com>
- To: GTK Applications <gtk-app-devel-list gnome org>
- Subject: Re: Unable to get Python/Gtk code to work...
- Date: Wed, 14 Oct 2009 13:28:14 -0700
On 10/12/2009 12:45 PM, Daniel B. Thurman wrote:
[snip!]
Here is updated code. The problem I have is preventing
the expand/collapse arrow button from changing the
Cell Rendering from shifting over the Icon/directory.
Try the code below, double-click the directories to
expand, then notice that the entire Directory column
is aligned together which is not what I want.
Suggestions please?
============ New Code ==============
[code]
#!/usr/bin/env python
import os, stat, time
import pygtk
pygtk.require('2.0')
import gtk
DEBUG=True
class FileLister:
# column_names = ['Name', 'Size', 'Mode', 'Last Changed', 'Path']
column_names = ['Files']
# Close the window and quit
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def __init__(self, path=None):
cell_data_funcs = (
None,
self.file_size,
self.file_mode,
self.file_last_changed,
self.file_path)
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("FileLister")
self.window.set_size_request(600, 400)
self.window.connect("delete_event", self.delete_event)
# Create TreeStore File List
self.treestore = gtk.TreeStore(str, gtk.gdk.Pixbuf, int, bool, str)
# Obtain the list of files
self.path = os.path.expanduser('~/Desktop/')
self.dir_walk(self.path)
# Create the TreeView and set our data store as the model
self.treeview = gtk.TreeView(self.treestore)
# self.treeview.set_search_column(0)
# self.treeview.set_reorderable(True)
# create the TreeViewColumns to display the data
self.tvcolumn = [None] * len(self.column_names)
cellpb = gtk.CellRendererPixbuf()
self.tvcolumn[0] = gtk.TreeViewColumn(self.column_names[0], cellpb)
self.tvcolumn[0].set_cell_data_func(cellpb, self.file_pixbuf)
cell = gtk.CellRendererText()
self.tvcolumn[0].pack_start(cell, False)
self.tvcolumn[0].set_cell_data_func(cell, self.file_name)
self.treeview.append_column(self.tvcolumn[0])
# Append more columns if available
for n in range(1, len(self.column_names)):
cell = gtk.CellRendererText()
self.tvcolumn[n] = gtk.TreeViewColumn(self.column_names[n],
cell)
if n == 1:
cell.set_property('xalign', 0.0)
self.tvcolumn[n].set_cell_data_func(cell, cell_data_funcs[n])
self.treeview.append_column(self.tvcolumn[n])
# Folder open action
self.treeview.connect('row-activated', self.on_row_activated)
# Add scrolled Window
self.scrolledwindow = gtk.ScrolledWindow()
self.scrolledwindow.add(self.treeview)
# Add scrolled window and show
self.window.add(self.scrolledwindow)
self.window.show_all()
# Obtain the directory listing
def dir_walk(self, path, parent=None, recurse=False):
filestat = os.stat(path)
if not stat.S_ISDIR(filestat.st_mode):
self.debug("File : "+path)
return self.treestore
for f in os.listdir(path):
filename = os.path.join(path, f)
self.debug("Directory: "+filename)
fdata = os.stat(filename)
is_folder = stat.S_ISDIR(fdata.st_mode)
img = gtk.icon_theme_get_default().load_icon(
"folder" if is_folder else "document",
gtk.ICON_SIZE_MENU, 0)
ts = self.treestore.append(parent, [f,img,fdata.st_size,
is_folder, path])
if is_folder & recurse:
self.dir_walk(filename, ts)
return self.treestore
def on_row_activated(self, treeview, path, column):
model = treeview.get_model()
iter = model.get_iter(path)
filename = os.path.join(model.get_value(iter, 4),
model.get_value(iter, 0))
#self.debug("File : "+filename)
ts = self.dir_walk(filename, parent=iter)
treeview.set_model(ts)
return
def file_pixbuf(self, column, cell, model, iter):
filename = os.path.join(model.get_value(iter, 4),
model.get_value(iter, 0))
filestat = os.stat(filename)
if stat.S_ISDIR(filestat.st_mode):
pb = gtk.icon_theme_get_default().load_icon("folder",
gtk.ICON_SIZE_MENU, 0)
else:
pb = gtk.icon_theme_get_default().load_icon("document",
gtk.ICON_SIZE_MENU, 0)
cell.set_property('pixbuf', pb)
return
def file_name(self, column, cell, model, iter):
cell.set_property('text', model.get_value(iter, 0))
return
def file_size(self, column, cell, model, iter):
filename = os.path.join(model.get_value(iter, 4),
model.get_value(iter, 0))
filestat = os.stat(filename)
cell.set_property('text', filestat.st_size)
return
def file_mode(self, column, cell, model, iter):
filename = os.path.join(model.get_value(iter, 4),
model.get_value(iter, 0))
filestat = os.stat(filename)
cell.set_property('text', oct(stat.S_IMODE(filestat.st_mode)))
return
def file_last_changed(self, column, cell, model, iter):
filename = os.path.join(model.get_value(iter, 4),
model.get_value(iter, 0))
filestat = os.stat(filename)
cell.set_property('text', time.ctime(filestat.st_mtime))
return
def file_path(self, column, cell, model, iter):
filename = os.path.join(model.get_value(iter, 4),
model.get_value(iter, 0))
cell.set_property('text', filename)
return
def debug(self, string):
if DEBUG:
print 'DEBUG: %s' % string
def main():
gtk.main()
if __name__ == "__main__":
myApp = FileLister()
main()
[/code]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]