AddMany used to layout buttons in frame. How to I capture events when they are clicked?

144 Views Asked by At
    import wx

class FrontMenu(wx.Frame):

    def __init__(self, parent, title):
        super(FrontMenu, self).__init__(parent, title=title, size=(400, 300))
        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        menubar = wx.MenuBar()
        fileMenu = wx.Menu()
        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)
        vbox = wx.BoxSizer(wx.VERTICAL)
        gs = wx.GridSizer(4, 1, 0, 0)

        gs.AddMany([
            (wx.Button(self, label='Create New Audit.'), 0, wx.EXPAND),
            (wx.Button(self, label='View Previous Audits.'), 0, wx.EXPAND),
            (wx.Button(self, label='Add New Engineer.'), 0, wx.EXPAND),
            (wx.Button(self, label='Close Application.'), 0, wx.EXPAND)
             ])

When using the AddMany method how do I capture button events? I can achieve this when I add the buttons 1 at a time.

Thanks Paul

2

There are 2 best solutions below

0
On

That pointed me in the right direction. Below is the code that now works.

Thanks

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(400,300))
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        menuBar = wx.MenuBar()
        menu = wx.Menu()
        m_exit = menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Close window and exit program.")
        self.Bind(wx.EVT_MENU, self.OnClose)
        self.Bind(wx.EVT_MENU, self.OnClose, m_exit)
        menuBar.Append(menu, "&File")
        menu = wx.Menu()
        m_about = menu.Append(wx.ID_ABOUT, "&About", "Information about this program")
        self.Bind(wx.EVT_MENU, self.OnAbout, m_about)
        menuBar.Append(menu, "&Help")
        self.SetMenuBar(menuBar)

        self.statusbar = self.CreateStatusBar()

        panel = wx.Panel(self)
        box = wx.GridSizer(2, 2)

        m_new_audit = wx.Button(panel, wx.NewId(), "New Audit.")
        m_new_audit.Bind(wx.EVT_BUTTON, self.NewAudit)
        m_view_audit = wx.Button(panel, wx.NewId(), "View Previous Audits.")
        m_view_audit.Bind(wx.EVT_BUTTON, self.ViewAudit)
        m_add_engineer = wx.Button(panel, wx.NewId(), "Add New Engineer.")
        m_add_engineer.Bind(wx.EVT_BUTTON, self.AddEngineer)
        m_close = wx.Button(panel, wx.NewId(), "Close.")
        m_close.Bind(wx.EVT_BUTTON, self.OnClose)
        box.Add(m_new_audit, 10, wx.EXPAND, 10)
        box.Add(m_view_audit, 10, wx.EXPAND, 10)
        box.Add(m_add_engineer, 10, wx.EXPAND, 10)
        box.Add(m_close, 10, wx.EXPAND, 10)


        panel.SetSizer(box)
        panel.Layout()

    def OnClose(self, event):
        dlg = wx.MessageDialog(self,
            "Do you really want to close this application?",
            "Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
        result = dlg.ShowModal()
        dlg.Destroy()
        if result == wx.ID_OK:
            self.Destroy()

    def NewAudit(self, event):
        dlg = wx.MessageDialog(self, 
            "Create New Audit?",
            "Confirm?", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
        result = dlg.ShowModal()
        if result == wx.ID_OK:
            event = "New Audit"
            print event
            dlg.Destroy()

    def ViewAudit(self, event):
        dlg = wx.MessageDialog(self,
            "View Previous Audit?",
            "Confirm?", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
        result = dlg.ShowModal()
        if result == wx.ID_OK:
            event = "Previous Audit"
            print event
            dlg.Destroy()

    def AddEngineer(self, event):
        dlg = wx.MessageDialog(self,
            "Add New Engineer?",
            "Confirm?", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
        result = dlg.ShowModal()
        if result == wx.ID_OK:
            event = "New Engineer"
            print event
            dlg.Destroy()

    def OnAbout(self, event):
        dlg = AboutBox()
        dlg.ShowModal()
        dlg.Destroy()
0
On

Assign ids to your buttons:

wx.Button(self, id=SOME_INTEGER, label=...

Add an event handler:

self.Bind(wx.EVT_BUTTON, self.on_button)

and in the handler check the id of the event source:

def on_button(self, evt):
    evt.Skip()
    if SOME_INTEGER == evt.GetId():
        # do something.