Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

right click on a tree view item

Help
2006-04-28
2013-05-28
  • chris johnson
    chris johnson
    2006-04-28

    Can you show or point me to some code that can demonstrate how to right click on a tree view item and select a menu item ?

     
    • Mark Mc Mahon
      Mark Mc Mahon
      2006-04-28

      Hi Chris,

      This is maybe not as easy as it should be :-( - but it is possible :-)

      I did some testing with Explorer...
      To get the following to work you need to have explorer opened with teh "My Documents" node in the tree view selected.

      from pywinauto.application import Application as App
      app = App.connect(title = "My Documents")
      # Get the 3rd item under desktop
      tree_item = app.TreeView.GetItem((0, 2))

      # find the rectangle of that item
      rect = tree_item.Rectangle()
      rect += app.TreeView.Rectangle()

      # Right click at this point
      app.MyDocs.RightClickInput(coords = (rect.left, rect.top))

      # Now there should be a popup menu
      app.PopupMenu.MenuItem("Open").Click()

      The above has been transcribed from one machine to this one by hand - so I could have left out something - so please be warned.

      While doing this - it seems clear that I might want to create a method that takes some form of control/item and clicks on it e.g.

      ClickInput(app.MyDocs.TreeView.GetItem((0,2)))

      This could work with anything that has a Rectangle attribute :-) so not so hard to create.

      Hope this answers your question - even if it is not as easy as could be hoped.

      Thanks
         Mark

       
    • Hi Mark!
      I am new at python and pywinauto and I have a similar problem.
      I want to make a rightclick on an item in the ListView of the explorer.
      But I am not able to access any Items because I have no valid window handle.
      Can you show me how I can get this handle?
      Or is there a better way?

      [code]
      from pywinauto import application

      app = application.Application()
      app.start("explorer.exe")
      exp = app.window_(title_re = ".*Eigene Dateien.*")

      #exp.Toolbox2.TypeKeys("%DC")
      item = exp.ListView.GetItem(0)
      ...
      [/code]

       
    • Mark Mc Mahon
      Mark Mc Mahon
      2006-05-18

      Hi,

      Unfortunately this is even harder then it is for TreeViews :-(

      Until I started answering your question I didn't have any code that could retrieve a listview item rect.

      I just added a ListViewWrapper.GetItemRect() method.

      Usage might be something like..

      from pywinauto.application import Application as App
      app = App.Connect(title = r"C:\.temp")
      item_coords = (app.Ctemp.ListView.GetItemRect(2).left + app.Ctemp.ListView.GetItemRect(2).right)/2, (app.Ctemp.ListView.GetItemRect(2).top + app.Ctemp.ListView.GetItemRect(2).bottom)/2

      app.Ctemp.ListView.RightClickInput(coords = item_coords)

      This works :-)

      I will try and release this version soon.

      Thanks
         Mark

       
    • Thanks for your help!
      Great work! :)

       
    • AlexSea
      AlexSea
      2007-07-26

      mark-

      Can you explain this part of code

      # find the rectangle of that item
      rect = tree_item.Rectangle()
      rect += app.TreeView.Rectangle()

      what happens when in the second line?

      I cannot figure out how to navigate the tree yet...