<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. <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 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> # close the window and quit<br> def delete_event(self, widget, event, data=None):<br> gtk.main_quit()<br> return False<br><br> def add_data(self):<br><br> # we'll add some data now - 4 rows with 3 child rows each<br> for parent in range(4):<br> piter = self.treestore.append(None, ['parent %i' % parent, 'value %i' %parent])<br> #print "this is path of parent " + str(parent) <br> #print self.treestore.get_path(piter)<br><br> for child in range(3):<br> citr = self.treestore.append(piter, ['parent %i child %i' % (parent, child, ), 'value %i' %parent])<br> #print "this is path of parent " + str(parent) + "child " + str(child)<br> #print self.treestore.get_path(citr)<br><br><br> def selectTest(self, widget, event):<br> print "This is event button"<br> print event.button<br> # Mouse press confirmed ok<br> if event.button == 1:<br> <br> #get data from highlighted selection <br> treeselection = self.treeview.get_selection()<br><br> (model, iter) = treeselection.get_selected()<br> if iter != None :<br> print "this is path of selected " <br><br> print self.treestore.get_path(iter)<br> print "value or select store"<br> print self.treestore.get_value(iter, 0)<br> print model.get_value(iter, 0)<br><br> else:<br> print "no selection made"<br><br> def __init__(self):<br> # Create a new window<br> self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)<br><br> self.window.set_title("Music Collection")<br><br> self.window.set_size_request(300, 200)<br><br> self.window.connect("delete_event", self.delete_event)<br> <br> # create a new scrolled window.<br> scrolled_window = gtk.ScrolledWindow()<br> scrolled_window.set_border_width(10)<br><br> # the policy is one of POLICY AUTOMATIC, or POLICY_ALWAYS.<br> # POLICY_AUTOMATIC will automatically decide whether you need<br> # scrollbars, whereas POLICY_ALWAYS will always leave the scrollbars<br> # there. The first one is the horizontal scrollbar, the second, the<br> # vertical.<br> scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)<br><br> # The dialog window is created with a vbox packed into it.<br> #self.window.vbox.pack_start(scrolled_window, True, True, 0)<br> scrolled_window.show()<br><br> # create a TreeStore with one string column to use as the model<br> self.treestore = gtk.TreeStore(str, str)<br> self.add_data()<br><br> # create the TreeView using liststore<br> self.treeview = gtk.TreeView(self.treestore)<br> self.treeview.set_headers_visible(False)<br><br> # create the TreeViewColumn to display the data<br> self.tvcolumn = gtk.TreeViewColumn()<br><br> # add tvcolumn to treeview<br> self.treeview.append_column(self.tvcolumn)<br><br> # create a CellRendererText to render the data<br> self.cell = gtk.CellRendererText()<br><br> # add the cell to the tvcolumn and allow it to expand<br> self.tvcolumn.pack_start(self.cell, True)<br><br> # set the cell "text" attribute to column 0 - retrieve text<br> # from that column in treestore<br> self.tvcolumn.add_attribute(self.cell, 'text', 0)<br><br> # make it searchable<br> self.treeview.set_search_column(0)<br> <br> self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK)<br> self.treeview.connect('button_press_event', self.selectTest)<br><br> # Allow sorting on the column<br> #self.tvcolumn.set_sort_column_id(0)<br><br> # Allow drag and drop reordering of rows<br> self.treeview.set_reorderable(True)<br><br> # pack the table into the scrolled window<br> scrolled_window.add_with_viewport(self.treeview)<br> self.window.add(scrolled_window)<br> self.window.show_all()<br><br>def main():<br> gtk.main()<br><br>if __name__ == "__main__":<br> tvexample = BasicTreeViewExample()<br> 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>