<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
Hi <br><br>I am new to GTK programming. I want to create a program that displays the sub-folders of a main folder in a tree view format. Once I find the particular sub-folder , I want to be able to click on it to display the contents of that folder. When the contents of that folder is displayed, I want to click on any file to display the mp3 tags of that file.<br><br>I have managed to get a program working that displays all the sub-folders of the main folder as a treeview. The problem is I am having trouble trying to get it to do the next step i.e. when I click on a sub-folder I want to display the contents of that folder.<br><br>After googling around, I found a post that used a solution of adding a button event to the treeview. I put it in an example code below and it works but with a problem.&nbsp; <br><br>Firstly, to describe the actions of the example code, I've created a treeeview that has 3 parents and 3 childs, when I click on a selection (either parent or child), the value of that selection is printed in the console.<br><br>The problem is that when I click on any selection, nothing happens. When I click on it again, the value of that selection is displayed. When I click on another selection, the value of the previous selection is displayed. When I click on that selection again, the values are displayed. On clicking on any selection, the value of the previous one is displayed.<br><br>I've put console print statements throughout the code and the contents are definately being displayed when I single click on selection. I can only think it is something to do with the lines&nbsp; treeselection = self.treeview.get_selection(), (model, iter) = treeselection.get_selected(). <br><br>I've checked the API and in TreeSelection there is text that may have something to do my issue:<br><br>"One of the important things to remember when monitoring the selection
of a view is that the "changed" signal is mostly a hint. That is, it
may only emit one signal when a range of rows is selected.
Additionally, it may on occasion emit a "changed" signal when nothing
has happened (mostly as a result of programmers calling the <a class="link" href="file:///home/theman/documents/python/pygtkdoc/www.pygtk.org/docs/pygtk/class-gtktreeselection.html#method-gtktreeselection--select-path" title="gtk.TreeSelection.select_path"><code class="methodname">select_path</code>()</a> or <a class="link" href="file:///home/theman/documents/python/pygtkdoc/www.pygtk.org/docs/pygtk/class-gtktreeselection.html#method-gtktreeselection--select-iter" title="gtk.TreeSelection.select_iter"><code class="methodname">select_iter</code>()</a> methods on an already selected row)."<br><br>I don't really know enough about GTK programming to understand where to start to look to fix this. Can someone please point me in the right direction?<br><br><br># #######Start of file<br>#!/usr/bin/env python<br><br><br>import pygtk<br>pygtk.require('2.0')<br>import gtk<br>import os<br><br>class BasicTreeViewExample:<br><br>&nbsp;&nbsp;&nbsp; # close the window and quit<br>&nbsp;&nbsp;&nbsp; def delete_event(self, widget, event, data=None):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gtk.main_quit()<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return False<br><br>&nbsp;&nbsp;&nbsp; def add_data(self):<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # we'll add some data now - 4 rows with 3 child rows each<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for parent in range(4):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; piter = self.treestore.append(None, ['parent %i' % parent,&nbsp; 'value %i' %parent])<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #print "this is path of parent " + str(parent) <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #print self.treestore.get_path(piter)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for child in range(3):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; citr = self.treestore.append(piter, ['parent&nbsp; %i child %i' % (parent, child, ),&nbsp; 'value %i' %parent])<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #print "this is path of parent " + str(parent) + "child " + str(child)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #print self.treestore.get_path(citr)<br><br><br>&nbsp;&nbsp;&nbsp; def selectTest(self, widget, event):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "This is event button"<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print event.button<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # Mouse press confirmed ok<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if event.button == 1:<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #get data from highlighted selection&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; treeselection = self.treeview.get_selection()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (model, iter) = treeselection.get_selected()<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if iter != None :<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "this is path of selected " <br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print self.treestore.get_path(iter)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "value or select store"<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print self.treestore.get_value(iter, 0)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print model.get_value(iter, 0)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else:<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print "no selection made"<br><br>&nbsp;&nbsp;&nbsp; def __init__(self):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # Create a new window<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.window.set_title("Music Collection")<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.window.set_size_request(300, 200)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.window.connect("delete_event", self.delete_event)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # create a new scrolled window.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scrolled_window = gtk.ScrolledWindow()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scrolled_window.set_border_width(10)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # the policy is one of POLICY AUTOMATIC, or POLICY_ALWAYS.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # POLICY_AUTOMATIC will automatically decide whether you need<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # scrollbars, whereas POLICY_ALWAYS will always leave the scrollbars<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # there. The first one is the horizontal scrollbar, the second, the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # vertical.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # The dialog window is created with a vbox packed into it.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #self.window.vbox.pack_start(scrolled_window, True, True, 0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scrolled_window.show()<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # create a TreeStore with one string column to use as the model<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treestore = gtk.TreeStore(str, str)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.add_data()<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # create the TreeView using liststore<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview = gtk.TreeView(self.treestore)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview.set_headers_visible(False)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # create the TreeViewColumn to display the data<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.tvcolumn = gtk.TreeViewColumn()<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # add tvcolumn to treeview<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview.append_column(self.tvcolumn)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # create a CellRendererText to render the data<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.cell = gtk.CellRendererText()<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # add the cell to the tvcolumn and allow it to expand<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.tvcolumn.pack_start(self.cell, True)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # set the cell "text" attribute to column 0 - retrieve text<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # from that column in treestore<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.tvcolumn.add_attribute(self.cell, 'text', 0)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # make it searchable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview.set_search_column(0)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview.connect('button_press_event', self.selectTest)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # Allow sorting on the column<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #self.tvcolumn.set_sort_column_id(0)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # Allow drag and drop reordering of rows<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.treeview.set_reorderable(True)<br><br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # pack the table into the scrolled window<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scrolled_window.add_with_viewport(self.treeview)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.window.add(scrolled_window)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.window.show_all()<br><br>def main():<br>&nbsp;&nbsp;&nbsp; gtk.main()<br><br>if __name__ == "__main__":<br>&nbsp;&nbsp;&nbsp; tvexample = BasicTreeViewExample()<br>&nbsp;&nbsp;&nbsp; main()<br><br>
# #######End of file<br><br><br /><hr />Share your photos with Windows Live Photos – Free  <a href='http://clk.atdmt.com/UKM/go/132630768/direct/01/' target='_new'>Find out more!</a></body>
</html>