I created a simple app (using wxGlade) consisting of a wxFrame containing a
wxCombobox. I bound EVT_COMBOBOX to a handler method. However each time I
selected an item in the combobox, the handler was called twice.
I found some examples on the Wiki and they didn't produce this behaviour.
Eventually I found that the significant difference was that the Wiki
examples had a wxPanel between the frame and the combobox.
After adding a panel to my app, the handler method is now only called once
each time an item is selected.
This behaviour has been seen on wxPython-2.5.2.8/Python-2.3.4/Win2K and
WinXP.
Similar behaviour was seen on wxPython-2.5.1.5/Python-2.3.4/HP-UX11i - but
here the handler was being called first when the combobox's drop down list
was displayed and again when an item was selected from the list. I haven't
tested if inserting a panel fixes it on HP-UX yet.
Here is the example that calls the handler twice:
···
#--------------------------------------------
#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
# generated by wxGlade 0.3.4.2 on Tue Sep 28 19:55:59 2004
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.combo_box_1 = wx.ComboBox(self, -1, choices=["one", "two",
"three"], style=wx.CB_DROPDOWN)
self.__set_properties()
self.__do_layout()
# end wxGlade
self.Bind(wx.EVT_COMBOBOX, self.OnCombo, self.combo_box_1)
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("frame_1")
self.combo_box_1.SetSize((100, 21))
self.combo_box_1.SetSelection(-1)
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.combo_box_1, 0, wx.FIXED_MINSIZE, 0)
self.SetAutoLayout(1)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Layout()
# end wxGlade
def OnCombo(self, event):
print event.GetString()
# end of class MyFrame
class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
self.SetTopWindow(frame_1)
frame_1.Show(1)
return 1
# end of class MyApp
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
#--------------------------------------------
Is this behaviour known? Any idea why it happens?
--
regards
Richard Townsend