Creating the final hellopy program

Now that you've got the basics under your belt, let's create the final version of the program we showed at the beginning of this chapter. Create a file named and enter in the code shown in listing 1.3.

Listing 1.3 The final

#!/usr/bin/env python 0 Shebang

Hello, wxPython! program

import wx

Docstring describes the code i— Import the wxPackage

A wx.Frame subclass class Frame(wx.Frame):

"""Frame class that displays an image."""

def _init_(self, image, parent=None, id=-1, d Image parameter pos=wx.DefaultPosition, title='Hello, wxPython!'): """Create a Frame instance and display image.""" 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) A wx.App subclass class App(wx.App):

Application class

Displaying the image def OnInit(self):

image = wx.Image('wxPython.jpg' self.frame = Frame(image)

Image handling g wx.BITMAP TYPE JPEG) ^

self.frame.Show() self.SetTopWindow(self.frame) return True def main():

main() function

Import vs. execute

O The shebang line allows this program to be an executable script under Linux and other Unix-like operating systems.

C Defining a custom Frame class that subclasses wx.Frame lets us more easily control the Frame's contents and appearance.

d We added an image parameter to our Frame's constructor. This value is provided by our application class when it creates a Frame instance. As long as we can pass the required values to wx.Frame._init_(), there's no reason we can't add more parameters to our subclass's constructor.

O We're going to display the image in a wx.StaticBitmap control, which requires a bitmap. So we convert the image to a bitmap. We also create a size tuple, using the width and height of the bitmap. The size tuple is supplied to the wx.Frame._

init_() call, so that the frame size matches the bitmap size.

Q Defining a wx.App subclass with an Onlnit() method is a minimum requirement for any wxPython application.

® We create an image object, using a wxPython.jpg file stored in the same directory as You can get this file from the Manning web site, or substitute one of your own. A more sophisticated version of this program would accept the name of a file from the command line. We pass our image object as a parameter when we create the frame.

Q The main() function creates an application instance and starts the wxPython event loop.

© Checking if this module is the main module allows it to be used in two different ways: run from the command line or imported by another module.

What happened when you ran your version of Did you see a frame sized to match the graphic you provided? If not, brush yourself off and try again. If so, congratulations! You're ready to move on to the next exciting steps.

But before you rush into the next chapter, we're going to talk about wxPython a little more broadly, what it's capable of, and how it came to be. If that doesn't interest you, feel free to jump to the next chapter and continue coding—the rest of the introduction will still be here.

Was this article helpful?

0 0

Post a comment