2015-06-15 53 views
0
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) 
      ]) 

當使用AddMany方法我怎麼捕捉按鍵事件? 當我一次添加按鈕1時,我可以達到此目的。AddMany用於佈局框架中的按鈕。如何在點擊時捕獲事件?

感謝 保羅

回答

0

分配ID給您的按鈕:

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

添加事件處理程序:

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

,並在處理程序檢查事件源ID:

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

這使我指出了正確的方向。以下是現在可用的代碼。

感謝

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()