Unable to get Python/Gtk code to work...



I am a first timer here and I am trying to get a
basic TreeView application to work on Fedora-11

I have gotten the following code from somewhere
on the Internet and needed this type of application
for a project I am working on.

The code I had obtained had a couple of problems
when dealing with file-types and was throwing an
exception in the places where I have added my DBT
comments, but even then, I was not able to get the
code to display the application and there is no error
thrown or reported as far as I can tell.

Any pointers/help would be appreciated!

Here is the following code:
============================================
#!/usr/bin/env python

import os, stat, gtk

# Instantiate the tree store and specify the data types
store = gtk.TreeStore(str, gtk.gdk.Pixbuf, int, bool)

def dirwalk(path, parent=None):
  # Iterate over the contents of the specified path
  for f in os.listdir(path):
    # Get the absolute path of the item
    fullname = os.path.join(path, f)
    # Extract metadata from the item
    # DBT: Added try/except because certain files will cause an exception!
    try:
       fdata = os.stat(fullname)
    except:
        continue

    # Determine if the item is a folder
    is_folder = stat.S_ISDIR(fdata.st_mode)
    # Generate an icon from the default icon theme
    img = gtk.icon_theme_get_default().load_icon(
        "folder" if is_folder else "document",
        gtk.ICON_SIZE_MENU, 0)
    # Append the item to the TreeStore
    # DBT: Added try/except because certain files will cause an exception!
    try:
        li = store.append(parent, [f, img, fdata.st_size, is_folder])
    except:
        continue
    # If the item is a folder, descend into it
    if is_folder:
        dirwalk(fullname, li)

#defautPath = os.path.expanduser('~')
defaultPath = "/home/user/wherever"
dirwalk(defaultPath)

# Create a TreeViewColumn
col = gtk.TreeViewColumn("File")
# Create a column cell to display text
col_cell_text = gtk.CellRendererText()
# Create a column cell to display an image
col_cell_img = gtk.CellRendererPixbuf()
# Add the cells to the column
col.pack_start(col_cell_img, False)
col.pack_start(col_cell_text, True)
# Bind the text cell to column 0 of the tree's model
col.add_attribute(col_cell_text, "text", 0)
# Bind the image cell to column 1 of the tree's model
col.add_attribute(col_cell_img, "pixbuf", 1)

col2 = gtk.TreeViewColumn("Size")
col2_cell_text = gtk.CellRendererText()
col2.pack_start(col2_cell_text)
col2.add_attribute(col2_cell_text, "text", 2)

# Create the TreeView and set our data store as the model
tree = gtk.TreeView(store)
# Append the columns to the TreeView
tree.append_column(col)
tree.append_column(col2)

scroll = gtk.ScrolledWindow()
scroll.add(tree)

window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.add(scroll)
window.show_all()




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]