Some issue with right click in TreeCtrl

tarun tarundevnani at gmail.com
Wed May 7 08:25:12 EDT 2008


Hello All,

Please find the code for a simple wx.TreeCtrl code. Whenever I right click
on any item in the Tree, I see the function, 'OnSelChanged' gets called
twice.

What I've just done is that I've associated the function 'OnRightClick' with
wx.EVT_TREE_ITEM_RIGHT_CLICK. And in this function, I say
self.tree.SelectItem(evt.GetItem(),True) to select the item and then so some
action for right click.

Can any one help.

Thanks & Regards,
Tarun

*Please find the code in the attachment and also below:-*

import wx
tests = ["All Tests",
  ["Suite1",
    "test01",
             "test02",
             ["suite2","test03","test04"],
    "test05",
  ],
  ["Suite3",
    "test06",
    "test07"
  ],
  ["Suite4",
    "test08",
    "test09"
  ],
  "test10",
  "test11",
]
class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Simple Tree", size=(400,500))
        # Create the tree
        self.tree = wx.TreeCtrl(self)
        # Add a root node
        root = self.tree.AddRoot(tests[0])
        # Add nodes from our data set
        self.AddTreeNodes(root, tests, 0)
        # Bind some interesting events
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree)
        self.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.OnRightClick,
self.tree)
        # Expand the first level
        self.tree.Expand(root)

    def AddTreeNodes(self, father, aTestList,count):
        if type(aTestList) == type([]):
            l = len(aTestList)
            i = 0
            while i < l:
                if i == 0:
                    if count ==1:
                        father = self.tree.AppendItem(father, aTestList[i])
                else:
                    self.AddTreeNodes(father, aTestList[i],
1)
                i = i + 1
        if type(aTestList) == type(""):
            self.tree.AppendItem(father, aTestList)

    def OnRightClick(self, evt):
        self.tree.SelectItem(evt.GetItem(),True)
        print 'In OnRightClick Function...',self.GetItemText(evt.GetItem())

    def GetItemText(self, item):
        if item:
            return self.tree.GetItemText(item)
        else:
            return ""

    def OnSelChanged(self, evt):
        print "OnSelChanged:   ", self.GetItemText(evt.GetItem())

app = wx.PySimpleApp(redirect=True)
frame = TestFrame()
frame.Show()
app.MainLoop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080507/a2b72be2/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: my_test.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20080507/a2b72be2/attachment.ksh>


More information about the Python-list mailing list