pygtk how to get treeview row(name) from a popup menu

kamu anuroop.m at
Mon Aug 6 12:43:58 CEST 2007

hi all,
thanks in advance for your valuable reply.

I have a pygtk code. which is a simple tree view. and when i click on
any row of the tree view, i get a menu poped up. and i can click on
the options in the poped up menu. and it goes to a function. up to
this its fine with me. but now from this function, how do i get the
name of the tree row which i clicked. ?? I tried some get_path &
get_iter method and tried to pass it with the call. but i could not
get it. I pasted the test code which used. please correct me if i am
doin any mistake, though i am new to this language.
the code is pasted below.
in the function print_A(), i tried some things. ultimately i wanted to
get the name of the row in tree from this function. (ie: like 'CHILD 1
1'  or 'PARENT 2' )

any comments on these will be helpful to me. because i am very new to

#!/usr/bin/env python

# generic treeview

import pygtk
import gtk
import os

class test_tree:

   # close the window and quit
   def delete_event(self, widget, event, data=None):
       return False

   def create_menu(self, window,event, data=None):
#       merge = gtk.UIManager()
#       self.set_data("ui-manager", merge)
#       accel = gtk.AccelGroup(merge)
       item_factory = gtk.ItemFactory(gtk.Menu, "<main>", None)
#       window.add_accel_group(accel)
       self.item_factory = item_factory

       return item_factory.get_widget("<main>")

   def button_press_callback(self, treeview, event, data=None):
       if event.button == 3:
           x = int(event.x)
           y = int(event.y)
           time = event.time
           pthinfo = self.treeview.get_path_at_pos(x,y)
        #   TreeSelection =
          # tre = gtk.TreeSelection.get_treeview()
         #  print "***"
          # print treeselection
          # print "***"
           if pthinfo is not None:
               path,col,cellx,celly = pthinfo
               menu = self.create_menu(self,self.window,None)

           return 1
   def print_A(self, w, data):
       iter =
       #iter = self.treeview.get_path()
       #aa = self.treestore.get_path(iter)
       print iter

       return 1

   def print_B(self, w, data):
       print "B"
       return 1

   def print_C(self, w, data):
       print "C"
       return 1

   def print_D(self, w, data):
       print "D"
       return 1

   def print_E(self, w, data):
       print "E"
       return 1

   def __init__(self):
       self.menu_items = (
                 ( "/_A",     "<control>O", self.print_A, 0, None ),
                 ( "/_B",    "<control>I", self.print_B, 0, None ),
                 ( "/_C",    "<control>H", self.print_C, 0, None ),
                 ( "/_D",    "<control>F", self.print_D, 0, None ),
                 ( "/_E",    "<control>U", self.print_E, 0, None ),

       # Create a new window
       self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)


       self.window.set_size_request(200, 200)

       self.window.connect("delete_event", self.delete_event)

       # create a TreeStore with one string column to use as the model
       self.treestore = gtk.TreeStore(str)

       # add some data now - 4 rows with 2 child rows each
       for parent in range(4):
           piter = self.treestore.append(None, ['PARENT %i' %
           for child in range(1):
               self.treestore.append(piter, ['CHILD1 %i' % parent])
               self.treestore.append(piter, ['CHILD2 %i' % parent])

       # create the TreeView using treestore
       self.treeview = gtk.TreeView(self.treestore)

       self.treeview.connect ("button_press_event",
self.button_press_callback, None)
       iter = self.treestore.get_iter_first()

       # create the TreeViewColumn to display the data
       self.tvcolumn = gtk.TreeViewColumn('project')

       # add tvcolumn to treeview

       # create a CellRendererText to render the data
       self.cell = gtk.CellRendererText()

       # add the cell to the tvcolumn and allow it to expand
       self.tvcolumn.pack_start(self.cell, True)

       # set the cell "text" attribute to column 0 - retrieve text
       # from that column in treestore
       self.tvcolumn.add_attribute(self.cell, 'text', 0)

       # make it searchable

       # Allow sorting on the column

       # Allow drag and drop reordering of rows



def main():

if __name__ == "__main__":
   test_tree = test_tree()

-thanks a lot

More information about the Python-list mailing list