Some issue with right click in TreeCtrl

T

tarun

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)
else:
self.AddTreeNodes(father, aTestList,
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top