EVT_TREE_SEL_CHANGED gets fired twice

Hi!
I have one strange problem. In wxTreeCtrl, if SelectItem is called right
after AppendItem, the SelectionChanged event gets fired twice. Already on
second day of tracing this problem, but no success so far.
Before reducing the application to bare minimum, SelectionChanged event
also was fired twice when left-clicking tree items with mouse, even the
ones already selected. Now this only happens after SelectItem() call.

Environment: wxPython 2.4.0.7u, Python 2.2.2, Windows2000 Professional SP2

The code to repeat the problem is given below. It messages prints to a
file whenever a node is appended or selection is changed.

Peteris underscore Martinsons reachable at exigengroup dot lv

···

#-------------------------------
from wxPython.wx import *
import time

class MainWindow(wxFrame):
    def __init__(self):
        wxFrame.__init__(self, None, -1, "frame", size=wxSize(400, 300),
style=wxDEFAULT_FRAME_STYLE)

        self.CreateToolBar(style=wxTB_FLAT)
        self._init_ToolBar(self.GetToolBar())

        style =
wxNO_3D|wxTR_HAS_BUTTONS|wxTR_FULL_ROW_HIGHLIGHT|wxTR_HIDE_ROOT|wxTR_ROW_LINES|wxTR_LINES_AT_ROOT

        self.t1 =t1= wxTreeCtrl(self, 7003, style=style)

        EVT_MENU(self, 7004, self.OnAdd)
        EVT_TREE_SEL_CHANGED(self.t1, self.t1.GetId(),
self.OnSelectionChanged)

    def _init_ToolBar(self, bar):
        bar.SetToolBitmapSize(wxSize(32, 32))
        bmp=wxEmptyBitmap(32, 32, depth = -1)
        bar.AddSeparator()
        bar.AddTool(id=7004, bitmap=bmp, pushedBitmap=wxNullBitmap)#
        bar.Realize()

    def OnAdd(self, event):
        im = self.AddNode()
        print "Add node"
        # Next line generates two Selection Change events
        self.t1.SelectItem(im)

    def AddNode(self):
        parent = self.t1.GetRootItem()
        newitem = self.t1.AppendItem(parent, "nodename")
        return newitem

    def OnSelectionChanged(self, event):
        print "---- selection change event -----"

class TheApp(wxApp):
    def OnInit(self):
        self.main = MainWindow()
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

if __name__ == '__main__':
    application = TheApp(true, "wxout.txt")
    application.MainLoop()
#-------------------------------

Peteris_Martinsons@exigengroup.lv wrote:

Hi!
I have one strange problem. In wxTreeCtrl, if SelectItem is called right after AppendItem, the SelectionChanged event gets fired twice. Already on second day of tracing this problem, but no success so far.
Before reducing the application to bare minimum, SelectionChanged event also was fired twice when left-clicking tree items with mouse, even the ones already selected. Now this only happens after SelectItem() call.

Environment: wxPython 2.4.0.7u, Python 2.2.2, Windows2000 Professional SP2

Different versions of the MS Common controls will often behave quite differently regarding which events are sent when. Since wxWindows can't track all the differences double sending of events will sometimes happen, but that is better than not sending it at all in some cases. If it is a problem then an easy workaround is to remember which item the last event was for and then not do anything the second time around if it is the same item.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!