[wxpython-users] where we put the .png files?

Mike Driscoll mdriscoll at co.marshall.ia.us
Fri May 2 07:49:51 PDT 2008


Abdessamad EL GON-NOUNI wrote:
>
>
> 2008/5/1 Mike Driscoll <mdriscoll at co.marshall.ia.us 
> <mailto:mdriscoll at co.marshall.ia.us>>:
>
>     Abdessamad EL GON-NOUNI wrote:
>
>         It dosn't work, he gives me the msg "Can'l load image from
>         file 'Exit.png':files does not exist"
>
>
>     Make sure the path to your png is correct. Also, check out
>     wx.ArtProvider for standard icons. I use img2py if I need custom
>     icons because it's easier to embed them in my application.
>
> where do I can check this path?
>
>
>
>         I have another problem: when I want to bind an element from
>         the menubar to an event, the program doesn't recognize the
>         method, I have this error msg: 'MainWindow' object has no
>         attribute 'OnExit'
>
>
>     Are you talking about a menu event or a toolbar event? 
>
>  Both menu and toolbar
>  
>
>     Here's a generic way to create code for a toolbar (I created a ):
>
>     <code>
>
>     def createToolbar(self):
>       toolbar = self.CreateToolBar(wx.TB_3DBUTTONS|wx.TB_TEXT)
>       self.toolbar.SetToolBitmapSize((16,16))
>       print_ico = wx.ArtProvider.GetBitmap(wx.ART_PRINT,
>     wx.ART_TOOLBAR, (16,16))
>       printTool = self.toolbar.AddSimpleTool(wx.ID_ANY, print_ico,
>     'Print', 'Sends Timesheet to Default Printer')
>       self.Bind(wx.EVT_MENU, self.OnPrint, sendTool)        
>     toolbar.Realize()
>
>     </code>
>
>     And here's how I do menubar code for the same function as above:
>
>     <code>
>
>     menubar = wx.MenuBar()
>     fileMenu = wx.Menu()
>     printDefault_menu_item = fileMenu.Append(wx.ID_ANY,
>     '&Print\tCtrl+P', 'Prints to Default Printer')
>     self.Bind(wx.EVT_MENU, self.onPrintDefault, printDefault_menu_item)
>     menubar.Append(fileMenu, '&File')
>     self.SetMenuBar(menubar)
>
>     </code>
>
> this is my code:
> <code>
> import wx
> ID_OPEN=001
> ID_ABOUT=101
> ID_EXIT=110
>
> class MainWindow(wx.Frame):
>     def __init__(self, title):
>         wx.Frame.__init__(self, None, -1, title)
>         #self.CreateStatusBar() # A Statusbar in the bottom of the window
>         # Setting up the menu.
>         menubar = wx.MenuBar(wx.MB_DOCKABLE)
>         file = wx.Menu()
>         edit = wx.Menu()
>         help = wx.Menu()
>
>         file.Append(-1,'&New')
>         file.Append(-1, "&Open")
>         file.Append(-1, "&Save")
>         file.AppendSeparator()
>         file.Append(ID_EXIT,"E&xit"," Terminate the program")
>        
>         edit.Append(-1, '&Preferences')
>         edit.Append(-1, 'Sea&rch')
>        
>         menubar.Append(file, '&File')
>         menubar.Append(edit, '&Edit')
>         menubar.Append(help, '&Help')
>         self.SetMenuBar(menubar)
>        
>         #Tool Bar
>         toolbar = wx.ToolBar(self,-1,style = wx.TB_HORIZONTAL | 
> wx.NO_BORDER)
The wx.Bitmap code below is looking in the directory from which your 
script is running for the png files. If you're not storing those pngs in 
that directory, then it fails. Let's pretend that you have them stored 
at C:\MyPngs. Then to specify this, you would do this instead:

toolbar.AddSimpleTool(wx.ID_OPEN,wx.Bitmap("C:\\MyPngs\\Open.png", 
wx.BITMAP_TYPE_PNG),shortHelpString = "Ouvrir",longHelpString = "Ouvrir 
une confiiguration")

>         toolbar.AddSimpleTool(wx.ID_OPEN,wx.Bitmap("Open.png", 
> wx.BITMAP_TYPE_PNG),shortHelpString = "Ouvrir",longHelpString = 
> "Ouvrir une confiiguration")
>         toolbar.AddSimpleTool (wx.ID_CLOSE,wx.Bitmap("Exit.png", 
> wx.BITMAP_TYPE_PNG),shortHelpString = "Fermer",longHelpString = 
> "Fermer le fichier ouvert")
>         toolbar.AddSeparator()
>         toolbar.Realize()
>         self.SetToolBar(toolbar)
>   
<snip>

You can also it using the ArtProvider, like so:

open_ico = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, 
(16,16))
toolbar.AddSimpleTool(wx.ID_OPEN,open_ico,shortHelpString = 
"Ouvrir",longHelpString = "Ouvrir une confiiguration")

Finally, here's how to use img2py:

1) Navigate to your wxPython tools directory. Mine is located at: 
C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\tools
2) Copy your png files there (If you're using a command line to do the 
first 2 steps, then skip to step #5)
3) Open a command window (in Windows, go to Start-Run, and type cmd)
4) Change the directory to the one in step 1
5) Run the following command, changing the icon file name and the output 
filename as needed:
    python img2py.py -i myIcon.ico icon.py
6) Copy your newly created Python files to the location where your 
script is and import it in your main script. Then you can use the 
getIcon() method.

I actually blogged about this here: http://www.blog.pythonlibrary.org/?p=13

See also http://wiki.wxpython.org/WorkingWithToolBars

Mike




More information about the wxpython-users mailing list