Using a Pushed Event Handler

Hi all,

I just know I'm missing something incredibly simple and obvious, but...

I've gotten PushEventHandler to work with an imported menubar, but now when I trigger an event from the menu, none of my event definitions seem to be able to figure out their working context. For example, I use the following two modules:

<module: RPA>
import OpenMenu
class MainFrame(wxFrame):
    def __init__(self, *args, **kwds):
        FirstMenu = OpenMenu.OpenMenuBar()
        self.SetMenuBar(FirstMenu)
class Main(wxApp):
    def OnInit(self):
        frame_1 = MainFrame(None, -1, "")
        MenuHandler=OpenMenu.OpenMenuEvents(frame_1)
        frame_1.PushEventHandler(MenuHandler)
if __name__ == "__main__":
    rpa = Main(0)
    rpa.MainLoop()

<module: OpenMenu>
ID_New=wxNewId()
ID_Exit=wxNewId()
class OpenMenuBar(wxMenuBar):
    def __init__(self, *args, **kwds):
class OpenMenuEvents(wxEvtHandler):
    def __init__(self, parent):
        frame=parent
        wxEvtHandler.__init__(self)
        EVT_MENU(self, ID_New, self.OnFileNew)
        EVT_MENU(self, ID_Exit, self.OnFileExit)
    def OnFileNew(self, event):
        """File > New"""
        return
    def OnFileExit(self,event):
        """File > Exit"""
        self.Close()

When run, using the Exit command as my test, I get one of two errors depending on how I try to reference Close(). In the above example, I get OpenMenuEvents instance has no attribute 'Close". O.K., so I figure it's the self.reference causing the problem. But, I have tried frame_1.Close(), parent.Close(), frame.Close(), everything I can think of, and all I get is Global Variable 'xxx' not defined. I can't reference frame_1 directly, and I can't seem to pass it in as a parameter and either call the parameter directly or assign it to a local variable. I've also changed around the EVT_MENU(self, xx,xx) to replace 'self' with parent or frame_1 - also with no luck.

Can anyone explain how I apply the command 'Close()' to the frame 'frame_1' from inside the pushed event handler?

I have created a wxListCtrl and I have associated an image list with that control. The images appear fine when I add list items to the list control. They also appear fine when I programmatically select and item in the list control. However, when I left click on the list control the image goes blank and the item is selected in blue. I have no problem with the blue part, however I want the image to maintain even when I have selected the item. I have also tried to use SetItemImage(<itemindex>, 0, 0). 0 being the first item in my image list. However, this does not fix the issue. Thoughts?

Chris Rouse

Rick Zoerner wrote:

Hi all,

I just know I'm missing something incredibly simple and obvious, but...

I've gotten PushEventHandler to work with an imported menubar, but now when I trigger an event from the menu, none of my event definitions seem to be able to figure out their working context. For example, I use the following two modules:

<module: RPA>
import OpenMenu
class MainFrame(wxFrame):
    def __init__(self, *args, **kwds):
        FirstMenu = OpenMenu.OpenMenuBar()
        self.SetMenuBar(FirstMenu)
class Main(wxApp):
    def OnInit(self):
        frame_1 = MainFrame(None, -1, "")
        MenuHandler=OpenMenu.OpenMenuEvents(frame_1)
        frame_1.PushEventHandler(MenuHandler)
if __name__ == "__main__":
    rpa = Main(0)
    rpa.MainLoop()

<module: OpenMenu>
ID_New=wxNewId()
ID_Exit=wxNewId()
class OpenMenuBar(wxMenuBar):
    def __init__(self, *args, **kwds):
class OpenMenuEvents(wxEvtHandler):
    def __init__(self, parent):
        frame=parent

You need to save the reference to parent in self, like this:

         self.frame = parent

        wxEvtHandler.__init__(self)
        EVT_MENU(self, ID_New, self.OnFileNew)
        EVT_MENU(self, ID_Exit, self.OnFileExit)
    def OnFileNew(self, event):
        """File > New"""
        return
    def OnFileExit(self,event):
        """File > Exit"""
        self.Close()

Then you can access it here via self too:

         self.frame.Close()

···

When run, using the Exit command as my test, I get one of two errors depending on how I try to reference Close(). In the above example, I get OpenMenuEvents instance has no attribute 'Close". O.K., so I figure it's the self.reference causing the problem. But, I have tried frame_1.Close(), parent.Close(), frame.Close(), everything I can think of, and all I get is Global Variable 'xxx' not defined. I can't reference frame_1 directly, and I can't seem to pass it in as a parameter and either call the parameter directly or assign it to a local variable. I've also changed around the EVT_MENU(self, xx,xx) to replace 'self' with parent or frame_1 - also with no luck.

Can anyone explain how I apply the command 'Close()' to the frame 'frame_1' from inside the pushed event handler?

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

Christopher Rouse wrote:

I have created a wxListCtrl and I have associated an image list with that control. The images appear fine when I add list items to the list control. They also appear fine when I programmatically select and item in the list control. However, when I left click on the list control the image goes blank and the item is selected in blue. I have no problem with the blue part, however I want the image to maintain even when I have selected the item. I have also tried to use SetItemImage(<itemindex>, 0, 0). 0 being the first item in my image list. However, this does not fix the issue. Thoughts?

Chris Rouse

Platform, version and sample code please.

···

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