[wxPython] wxImage bitmap sample code

Does someone have a snippet of code illustrating how to load a bitmap
and display it in a frame using wxPython

I am new to wxPython and haven't got the relationship between wxImage,
wxDC, wxBitMap, wxDC::Blit and wxFrame all sorted out. I think if I
could see an example where a bitmap (or other image format) was loaded
from disk and displayed in a window, it would help me along.

What I understand crudely is that you load an image like

        image = wxImage('plane_A.bmp')

This has to be converted to a wxBitmap, which is then drawn into a
device context with wxDC::DrawBitmap and then this DC can be used to
draw it into your frame....

John Hunter

Does someone have a snippet of code illustrating how to load a bitmap
and display it in a frame using wxPython

wxPython/lib/splashscreen.py is one example.

···

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

this might help.
Bob

from wxPython.wx import *

ID_FILE_OPEN = 101
ID_FILE_EXIT = 103

class ImagePanel(wxPanel):
     def __init__(self, parent, id):
         wxPanel.__init__(self, parent, id)
         self.image = None
         EVT_PAINT(self, self.OnPaint)

     def display(self, image):
         self.image = image
         self.Refresh(true)

     def OnPaint(self, evt):
         dc = wxPaintDC(self)
         if self.image:
             dc.DrawBitmap(self.image.ConvertToBitmap(), 0,0)

···

At 11:10 AM 11/6/2001 -0600, you wrote:

Does someone have a snippet of code illustrating how to load a bitmap
and display it in a frame using wxPython

#----------------------------------------------------------------------

class MyFrame(wxFrame):
     def __init__(self, parent, ID, title):
         wxFrame.__init__(self, parent, ID, title, wxDefaultPosition,wxSize(500, 400))

         self.iPanel = ImagePanel(self, -1)
         self.im = None

         # Construct "File" menu
         self.menuBar = wxMenuBar()
         self.menuFile = wxMenu()
         self.menuFile.Append(ID_FILE_OPEN, "&Open image","")
         EVT_MENU(self, ID_FILE_OPEN, self.OnOpen)
         self.menuFile.AppendSeparator()
         self.menuFile.Append(ID_FILE_EXIT, "E&xit", "")
         EVT_MENU(self, ID_FILE_EXIT, self.OnExit)
         self.menuBar.Append(self.menuFile, "&File");
         self.SetMenuBar(self.menuBar)

         wxInitAllImageHandlers() # loads all available image handlers (image types)

     def OnOpen(self, event):
         f1 = 'All image files (tif, bmp, jpg, gif, png...)| \
             *.tif;*.bmp;*.jpg;*.png;*.pcx;*.gif;*.pnm'
         fileTypes = f1
         fd = wxFileDialog(self, "Open Image", "", "", fileTypes, wxOPEN)

         if fd.ShowModal() == wxID_OK:
             path = fd.GetPath()
             self.LoadImage(path)
         fd.Destroy()

     def LoadImage(self, path):
         try:
             self.im = wxImage(path, wxBITMAP_TYPE_BMP) # this would also work
             #self.im = wxNullImage()
             self.im.LoadFile(path, wxBITMAP_TYPE_ANY) # autodetects the format
             self.iPanel.display(self.im)
             return true
         except IOError:
             print "can't open the file"
             return false

     def OnCloseWindow(self, event):
         self.Destroy()

     def OnExit(self, event):
         self.Close(true)

#---------------------------------------------------------------------------

class MyApp(wxApp):
     def OnInit(self):
         frame = MyFrame(NULL, -1, "wxNoPilSimple")
         frame.Show(true)
         self.SetTopWindow(frame)
         return true

app = MyApp(0)
app.MainLoop()