Wednesday, October 8, 2008

The final hello application

In this section we'll add more feature with our previous application.we'll
1)Extend our previous MyFrame class
2)Add a image as the Frame background.
following is the source code.

### Another simple App with image###
import wx

class MyFrame(wx.Frame):
###We define a new Frame with many param,especially it could contain a image ###
def __init__(self,image,parent=None,id=-1,pos=wx.DefaultPosition,
title='Hello WxPython'):
temp=image.ConvertToBitmap()
size=temp.GetWidth(),temp.GetHeight()
wx.Frame.__init__(self,parent,id,title,pos,size)
self.bmp=wx.StaticBitmap(parent=self,bitmap=temp)

class MyApp(wx.App):
def OnInit(self):
image=wx.Image('c:/wxPython.jpg',wx.BITMAP_TYPE_JPEG)
self.frame=MyFrame(image)
self.frame.Show()
self.SetTopWindow(self.frame)
return True

if __name__ == '__main__':
app=MyApp()
app.MainLoop()
we have a wxPython.jpg file,we want to add it to Frame as a background.because of we'll use the wx.StaticBitmap component on the frame to display the image,so we need convert the jpeg file to bitmap file.the way is use ConvertToBitmap() method,then we get the width and height of image file and set it as frame's size.And then we need assign a param to our custom Frame class to tell it which image will be display.

No comments: