Binding mouse clicks to toolbar button

Hello all,

I have been following this list for a while, and everyone seems very helpful! I just started with wxPython, so hopefully this question will not prove too difficult--

I have a toolbar and a menubar currently in a very basic app. Right now, I want them to effectively be the same, i.e. each menu item will have a corresponding toolbar button that does the same thing. Portion of my code with the menubar and the toolbar:

        # Add a tool bar:
        toolbar = self.CreateToolBar()
        helpbutton = toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/help-browser.png',
            wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Help', 'Help for Domainspotter')
        self.Bind(wx.EVT_LEFT_DOWN, self.ShowHelp, helpbutton)
        exitbutton = toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/system-log-out.png',
            wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Exit', 'Long help for Exit')
        self.Bind(wx.EVT_LEFT_DOWN, self.OnCloseMe, exitbutton)
        toolbar.Realize()
        # Add a menu bar:
        menubar = wx.MenuBar()
        menu1 = wx.Menu()
        menubar.Append(menu1, "&File")
        exititem = menu1.Append(wx.NewId(), "&Exit", "Exit Domainspotter")
        self.Bind(wx.EVT_MENU, self.OnCloseMe, exititem)
        menu2 = wx.Menu()
        menubar.Append(menu2, "&Help")
        aboutitem = menu2.Append(wx.NewId(), "&About", "About Domainspotter")
        self.Bind(wx.EVT_MENU, self.ShowAbout, aboutitem)
        helpitem = menu2.Append(wx.NewId(), "Help", "Domainspotter Help")
        self.Bind(wx.EVT_MENU, self.ShowHelp, helpitem)
        self.SetMenuBar(menubar)

and then the associated functions mentioned in the Bind()'s:

    def OnCloseMe(self, event):
        self.Close(True)
    
    def ShowAbout(self, event):
        abouttext = wx.MessageDialog(self, self.abouttext, caption = "About Domainspotter",
            style = wx.OK | wx.ICON_INFORMATION)
        abouttext.ShowModal()
        abouttext.Destroy()
        
    def ShowHelp(self, event):
        helptext = wx.MessageDialog(self, self.helptext, caption = "Domainspotter Help",
            style = wx.OK | wx.ICON_INFORMATION)
        helptext.ShowModal()
        helptext.Destroy()

The menubar items work just fine. Help and about show help and about, and exit exits. But, when I press the Help or Exit buttons, which are bound to the same functions, nothing happens. No errors either.

My guess is that I have the wrong event specified in the self.Bind for the buttons. I tried wx.EVT_BUTTON as well, same deal.

Thanks in advance for the help,
Sam

Hi Sam,

···

On 9/25/07, wormwood_3 wrote:

       # Add a tool bar:
       toolbar = self.CreateToolBar()
       helpbutton = toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/help-browser.png',
           wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Help', 'Help for Domainspotter')
       self.Bind(wx.EVT_LEFT_DOWN, self.ShowHelp, helpbutton)
       exitbutton = toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/system-log-out.png',
           wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Exit', 'Long help for Exit')
       self.Bind(wx.EVT_LEFT_DOWN, self.OnCloseMe, exitbutton)
       toolbar.Realize()
       # Add a menu bar:
       menubar = wx.MenuBar()
       menu1 = wx.Menu()
       menubar.Append(menu1, "&File")
       exititem = menu1.Append(wx.NewId(), "&Exit", "Exit Domainspotter")
       self.Bind(wx.EVT_MENU, self.OnCloseMe, exititem)
       menu2 = wx.Menu()
       menubar.Append(menu2, "&Help")
       aboutitem = menu2.Append(wx.NewId(), "&About", "About Domainspotter")
       self.Bind(wx.EVT_MENU, self.ShowAbout, aboutitem)
       helpitem = menu2.Append(wx.NewId(), "Help", "Domainspotter Help")
       self.Bind(wx.EVT_MENU, self.ShowHelp, helpitem)
       self.SetMenuBar(menubar)

and then the associated functions mentioned in the Bind()'s:

   def OnCloseMe(self, event):
       self.Close(True)

   def ShowAbout(self, event):
       abouttext = wx.MessageDialog(self, self.abouttext, caption = "About Domainspotter",
           style = wx.OK | wx.ICON_INFORMATION)
       abouttext.ShowModal()
       abouttext.Destroy()

   def ShowHelp(self, event):
       helptext = wx.MessageDialog(self, self.helptext, caption = "Domainspotter Help",
           style = wx.OK | wx.ICON_INFORMATION)
       helptext.ShowModal()
       helptext.Destroy()

The menubar items work just fine. Help and about show help and about, and exit exits. But, when I press the Help or Exit buttons, which are bound to the same functions, nothing happens. No errors either.

My guess is that I have the wrong event specified in the self.Bind for the buttons. I tried wx.EVT_BUTTON as well, same deal.

Instead of wx.EVT_LEFT_DOWN, try to do something like:

self.Bind(wx.EVT_TOOL, self.OnCloseMe, id=exitbutton.GetId())

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

Think your looking for wx.EVT_TOOL

cody

···

On Sep 25, 2007, at 7:49 AM, wormwood_3 wrote:

Hello all,

I have been following this list for a while, and everyone seems very helpful! I just started with wxPython, so hopefully this question will not prove too difficult--

I have a toolbar and a menubar currently in a very basic app. Right now, I want them to effectively be the same, i.e. each menu item will have a corresponding toolbar button that does the same thing. Portion of my code with the menubar and the toolbar:

        # Add a tool bar:
        toolbar = self.CreateToolBar()
        helpbutton = toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/help-browser.png',
            wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Help', 'Help for Domainspotter')
        self.Bind(wx.EVT_LEFT_DOWN, self.ShowHelp, helpbutton)
        exitbutton = toolbar.AddSimpleTool(wx.NewId(), wx.Image('resources/system-log-out.png',
            wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Exit', 'Long help for Exit')
        self.Bind(wx.EVT_LEFT_DOWN, self.OnCloseMe, exitbutton)
        toolbar.Realize()
        # Add a menu bar:
        menubar = wx.MenuBar()
        menu1 = wx.Menu()
        menubar.Append(menu1, "&File")
        exititem = menu1.Append(wx.NewId(), "&Exit", "Exit Domainspotter")
        self.Bind(wx.EVT_MENU, self.OnCloseMe, exititem)
        menu2 = wx.Menu()
        menubar.Append(menu2, "&Help")
        aboutitem = menu2.Append(wx.NewId(), "&About", "About Domainspotter")
        self.Bind(wx.EVT_MENU, self.ShowAbout, aboutitem)
        helpitem = menu2.Append(wx.NewId(), "Help", "Domainspotter Help")
        self.Bind(wx.EVT_MENU, self.ShowHelp, helpitem)
        self.SetMenuBar(menubar)

and then the associated functions mentioned in the Bind()'s:

    def OnCloseMe(self, event):
        self.Close(True)

    def ShowAbout(self, event):
        abouttext = wx.MessageDialog(self, self.abouttext, caption = "About Domainspotter",
            style = wx.OK | wx.ICON_INFORMATION)
        abouttext.ShowModal()
        abouttext.Destroy()

    def ShowHelp(self, event):
        helptext = wx.MessageDialog(self, self.helptext, caption = "Domainspotter Help",
            style = wx.OK | wx.ICON_INFORMATION)
        helptext.ShowModal()
        helptext.Destroy()

The menubar items work just fine. Help and about show help and about, and exit exits. But, when I press the Help or Exit buttons, which are bound to the same functions, nothing happens. No errors either.

My guess is that I have the wrong event specified in the self.Bind for the buttons. I tried wx.EVT_BUTTON as well, same deal.

Thanks in advance for the help,
Sam

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

Cody Precord wrote:

Think your looking for wx.EVT_TOOL

EVT_TOOL is, for all intents and purposes, the same as EVT_MENU. So this enables a little trick that may not be readily apparent from Python (but is used often in C++) which is that if the menu items and their corresponding toolbar items have the same ID, then you can handle both with the same event binding. They can also be enabled/disabled with the same EVT_UPDATE_UI binding too.

To make use of this trick you can create one of the items first (either the menu item or the toolbar item,) and then use item.GetId() for the ID when you create the other. This will help simplify the code somewhat, but may not suit everybody's sense of style. :wink:

  item = menu.Append(wx.NewId(), "Blah", "blah, blah, blah")
  toolbar.AddSimpleTool(item.GetId(), bitmap, "Blah", "blah, blah, blah")
  self.Bind(wx.EVT_MENU, self.OnDoBlah, item)

I'm sure you can see how this could easily be put into it's own method to simplify things even further...

wx.Image('resources/help-browser.png',
            wx.BITMAP_TYPE_PNG).ConvertToBitmap(),

BTW, no need to create a wx.Image if you are just going to convert it to a bitmap. The wx.Bitmap constructor will do all of this itself if it needs to. This will work just as well, and is a lot less to type:

  wx.Bitmap('resources/help-browser.png')

···

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