how to add a toolbar to a Frame using wxpython

kyosohma at gmail.com kyosohma at gmail.com
Tue Jul 31 10:43:11 EDT 2007


On Jul 31, 8:24 am, yadin <conra2... at yahoo.com> wrote:
> hi
> please am learning wxpython and I Need help on getting examles of how
> to create a toolbar with images
> that is add a toolbar to a Frame using wxpython
> using wxpython on a windows pc
> how can i add a toolbar to a GUI
> all sample codes i could find contain errors.
> i did the following but it is wrong!
> my biggest trouble seems to be with the IMAGES? do i have to create
> them, load them...? or they come with python like in Matlab
> thank you.
>
> import wx
>
> from pylab import*
>
> class MainWindow(wx.Frame):
>         def __init__(self,parent,id,title):
>             self.dirname=''
>             wx.Frame.__init__(self,parent,wx.ID_ANY,
> title,wx.DefaultPosition, wx.Size(320,410))
>
>             ##############_______________________ADDING A
> TOOLBAR___________________######################
>
>             toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL |
> wx.NO_BORDER)
>             toolbar.AddSimpleTool(1, wx.Image('stock_new.png',
> wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'New', '')
>             toolbar.Realize()
>             Frame.Add(toolbar, 0, border=5)
>
> app = wx.PySimpleApp()
> frame = MainWindow(None, -1, " dipole antenna ")
> frame.show(1)
> frame.SetBackgroundColour('light grey')
> app.MainLoop()

The internal pictures that I've found so far for wxPython are listed
here: http://www.wxpython.org/docs/api/wx.ArtProvider-class.html

If you need to create your own icons / bitmaps, I use the img2py
utility included with wxPython. It's usage is below:

<code>
# on the command line in windows
# You have to pass a filename in for img2py to sends its output to
c:\Python24\Lib\site-packages\wx-2.8-msw-ansi\wx\tools>python
img2py.py -i iconFileName iconPyFile.py
</code>

Also see: http://www.wxpython.org/docs/api/wx.tools.img2py-module.html

Then I just created a function to create my toolbar:

<code>
def createToolbar(self):
        toolbar = self.CreateToolBar()
        toolbar.SetToolBitmapSize((31,31))
        bmp = mail_ico.getBitmap()
        sendTool = toolbar.AddSimpleTool(-1, bmp, 'Send', 'Sends
Email')
        self.Bind(wx.EVT_MENU, self.OnSend, sendTool)
        toolbar.Realize()
</code>

One caveat: I don't know how to force an image to scale to the size of
the toolbar button. You'll have to figure that out on your own.

You can also post to the wxPython list, which might give you more
coherent answers: http://www.wxpython.org/maillist.php

Mike




More information about the Python-list mailing list