Events disappearing in wxPython-2.6.3.3 and 2.8.0.1 ?

My first post to the list, so please bear with me.

I've inherited a wxPython application that was written for Python-2.4.3
and wxPython-2.4.2.4 and which still works on a frozen Lunar Linux box.

However, we've moved to CentOS 4.4 which has a different configuration.
It has Python-2.3 as default and our app needs 2.4 features, so two
weeks ago I installed Python-2.4.4 and wxPython-2.8.0.1 under my $HOME.
Unfortunately the wxPython application seems to lose menu and button
events and I couldn't fot the life of me see why. Eventually I had to
install wxPython-2.4.2.4 under $HOME instead in order to continue working.

I've scoured the online tutorial/getting started pages, and have distilled
the problem down to a relatively short Python program which works on the
CentOS-4.4 / Python-2.4.4 / wxPython-2.4.2.4 combination, but which shows
the same disappearing event problem on my private box at home which has
an up-to-date Lunar Linux / Python-2.5 / wxPython-2.6.3.3

Can anyone tell me what is likely to be wrong in the code below?
- Have I based the event handling on outdated or too new examples?
- Is the event handling at the wrong level? i.e in Frame rather than...
- Is this likely to be a problem with lower level library?

Or does it just work on your systems? On mine, none of the menu items
generates the expected print line, pops up the dialog, or closes the app.

Cheers
Duncan

import wx

class MainWindow(wx.Frame):

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        menuBar = self._createMenuBar()
        self.SetMenuBar(menuBar)

    def _createMenuBar(self):
        menuBar = wx.MenuBar()
        fileMenu = self._createFileMenu()
        menuBar.Append(fileMenu, "&File")
        return menuBar

    def _createFileMenu(self):
        fileMenu = wx.Menu()

        newId = wx.NewId()
        fileMenu.Append(newId, "&New", " new thingy ")
        wx.EVT_MENU(self, newId, self.OnNew)

        prefsId = wx.NewId()
        fileMenu.Append(prefsId, "&Preferences", " preferences thingy ")
        wx.EVT_MENU(self, prefsId, self.OnPreferences)

        exitId = wx.NewId()
        fileMenu.Append(exitId, "E&xit", " exit thingy ")
        wx.EVT_MENU(self, exitId, self.OnExit)

        return fileMenu

    def OnNew(self, unused_event):
        print 'MainWindow:OnNew'

    def OnPreferences(self, unused_event):
        print 'MainWindow:OnPreferences'
        d = wx.MessageDialog(self, 'Message Text', 'Title',
                wx.OK | wx.ICON_INFORMATION)
        d.ShowModal()
        d.Destroy()

    def OnExit(self, unused_event):
        print 'MainWindow:OnExit'
        self.Close()

def _main():
    app = wx.PySimpleApp(0)
    mainWindow = MainWindow(None, wx.ID_ANY, "Testing...")
    mainWindow.Show(1)
    app.MainLoop()

if __name__ == '__main__':
    _main()

Seems to work fine under Windows.
The only thing I can see about your code that could be wrong is the use of old-style binding. I don't know whether it has been deprecated, but the new style is cleaner anyway. So, instead of writing:

        wx.EVT_MENU(self, newId, self.OnNew)

You should write:

       self.Bind(wx.EVT_MENU, self.OnNew, id=newId)

Of even better, just store the menu item returned from the Append and use it to bind the event:

        newMenuItem = fileMenu.Append(-1, "&New", " new thingy ")
        self.Bind(wx.EVT_MENU, self.OnNew, newMenuItem)

This way you don't even have to generate the id's explicitly.

Duncan Gibson wrote:

···

My first post to the list, so please bear with me.

I've inherited a wxPython application that was written for Python-2.4.3
and wxPython-2.4.2.4 and which still works on a frozen Lunar Linux box.

However, we've moved to CentOS 4.4 which has a different configuration.
It has Python-2.3 as default and our app needs 2.4 features, so two
weeks ago I installed Python-2.4.4 and wxPython-2.8.0.1 under my $HOME.
Unfortunately the wxPython application seems to lose menu and button
events and I couldn't fot the life of me see why. Eventually I had to
install wxPython-2.4.2.4 under $HOME instead in order to continue working.

I've scoured the online tutorial/getting started pages, and have distilled
the problem down to a relatively short Python program which works on the
CentOS-4.4 / Python-2.4.4 / wxPython-2.4.2.4 combination, but which shows
the same disappearing event problem on my private box at home which has
an up-to-date Lunar Linux / Python-2.5 / wxPython-2.6.3.3

Can anyone tell me what is likely to be wrong in the code below?
- Have I based the event handling on outdated or too new examples?
- Is the event handling at the wrong level? i.e in Frame rather than...
- Is this likely to be a problem with lower level library?

Or does it just work on your systems? On mine, none of the menu items
generates the expected print line, pops up the dialog, or closes the app.

Cheers
Duncan

import wx

class MainWindow(wx.Frame):

    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        menuBar = self._createMenuBar()
        self.SetMenuBar(menuBar)

    def _createMenuBar(self):
        menuBar = wx.MenuBar()
        fileMenu = self._createFileMenu()
        menuBar.Append(fileMenu, "&File")
        return menuBar

    def _createFileMenu(self):
        fileMenu = wx.Menu()

        newId = wx.NewId()
        fileMenu.Append(newId, "&New", " new thingy ")
        wx.EVT_MENU(self, newId, self.OnNew)

        prefsId = wx.NewId()
        fileMenu.Append(prefsId, "&Preferences", " preferences thingy ")
        wx.EVT_MENU(self, prefsId, self.OnPreferences)

        exitId = wx.NewId()
        fileMenu.Append(exitId, "E&xit", " exit thingy ")
        wx.EVT_MENU(self, exitId, self.OnExit)

        return fileMenu

    def OnNew(self, unused_event):
        print 'MainWindow:OnNew'

    def OnPreferences(self, unused_event):
        print 'MainWindow:OnPreferences'
        d = wx.MessageDialog(self, 'Message Text', 'Title',
                wx.OK | wx.ICON_INFORMATION)
        d.ShowModal()
        d.Destroy()

    def OnExit(self, unused_event):
        print 'MainWindow:OnExit'
        self.Close()

def _main():
    app = wx.PySimpleApp(0)
    mainWindow = MainWindow(None, wx.ID_ANY, "Testing...")
    mainWindow.Show(1)
    app.MainLoop()

if __name__ == '__main__':
    _main()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Duncan Gibson wrote:

Can anyone tell me what is likely to be wrong in the code below?
- Have I based the event handling on outdated or too new examples?
- Is the event handling at the wrong level? i.e in Frame rather than...
- Is this likely to be a problem with lower level library?

Or does it just work on your systems? On mine, none of the menu items
generates the expected print line, pops up the dialog, or closes the app.

I don't see anything wrong in the code, and it does work for me with the 2.6 and 2.8 versions I tried it with.

···

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

Robin Dunn wrote:

I don't see anything wrong in the code, and it does work for me with the
2.6 and 2.8 versions I tried it with.

I went back to try the code with wxPython-2.6.3.3 on Lunar Linux,
and the events don't appear to get through. I tried Eli's suggestion
of replacing

        newId = wx.NewId()
        fileMenu.Append(newId, "&New", " new thingy ")
        wx.EVT_MENU(self, newId, self.OnNew)

with

        newMenuItem = fileMenu.Append(wx.ID_ANY, "&New", " new thingy ")
        self.Bind(wx.EVT_MENU, self.OnNew, newMenuItem)

but that didn't appear to make any difference. I've been though the loop
of trying to install wxPython-2.8.1.1 (again) and still couldn't get it
to work. All I can think of is that I'm missing some subsidiary library,
or I have an incompatible version. For example, I'm running Xfce and not
Gnome, so it could be that some option has been disabled somewhere along
the line, and I don't know whether I'll be able to work out what. Still,
it'll keep me off the streets at night :slight_smile:

Cheers
Duncan