Book Image

wxPython 2.8 Application Development Cookbook

By : Cody Precord
Book Image

wxPython 2.8 Application Development Cookbook

By: Cody Precord

Overview of this book

<p>In today’s world of desktop applications there is a great amount of incentive to be able to develop applications that can run in more than one environment. Currently there are a handful of options available for cross platform frameworks to develop desktop applications in Python. wxPython is one such cross- platform GUI toolkit for the Python programming language. It allows Python programmers to create programs with a complete, highly functional graphical user interface, simply and easily. wxPython code style has changed quite a bit over the years, and gotten much more Pythonic. The examples you will find in this book are right up to date and reflect this change in style.<br />This cookbook provides you with the latest recipes to quickly create robust, reliable, and reusable wxPython applications. These recipes will guide you from writing simple, basic wxPython scripts all the way through complex concepts, and also feature various design approaches and techniques in wxPython.<br /><br />The book starts off by covering a variety of topics from the most basic requirements of a wxPython application to some of the more in depth details of the inner workings of the framework laying the foundation for any wxPython application. It then explains event handling, basic and advanced user interface controls, designing and layout, creating dialogs, components and extending functionality, and so on. We conclude by learning how to build and manage applications for distribution.<br />For each of the recipes, there is an introductory example, then more advanced examples, and plenty of example code to develop and manage user-friendly applications. For more experienced developers, most recipes also include additional discussion of the solution, allowing you to further customize and enhance the component.</p>
Table of Contents (19 chapters)
wxPython 2.8 Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The main frame


For most applications, you will want to display a window for its users to interact with. In wxPython, the most typical window object is known as a Frame. This recipe will show you how to subass a Frame and display it in an application.

How to do it...

This example extends upon the previous recipe to add a minimal empty application window:

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title="The Main Frame")
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title="", 
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.DEFAULT_FRAME_STYLE,
                 name="MyFrame"):
        super(MyFrame, self).__init__(parent, id, title,
                                      pos, size, style, name)

        # Attributes
        self.panel = wx.Panel(self)

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()

Running the previous code will result in a window like the following being shown:

How it works...

The Frame is the main top-level window and container for most applications. Let's start by examining our MyFrame class. In this class there is one important thing to note. We created a Panel object as a child window of the Frame. You can think of a Panel as a box for containing other controls. Also, in order for a Frame to operate and look correct on all platforms, it is important that it has a Panel as its main child.

Firstly, in the OnInit method of our App, we create an instance of MyFrame, passing None as its first parameter. This parameter is used to specify the parent window of the Frame. Because this is our main window, we pass in None to indicate that it has no parent. Secondly, we call the SetTopWindow method of our App in order to set our newly-created MyFrame instance as the application's top window. Thirdly and finally, we call Show on our Frame; this simply does what its name suggests, and shows the Frame so that a user can see it, though the Frame will not actually be visible on the screen until the MainLoop is started.

There's more...

The Frame class has a number of style flags that can be set in its constructor to modify the behavior and appearance of the window. These style flags can be combined as a bitmask and are supplied as the value to the constructors' style parameter. The following table outlines some of the common ones. A full list of all available styles can be found in the wxPython online documentation, at http://wxpython.org/onlinedocs.php.

Style flags

Description

wx. DEFAULT_FRAME_STYLE

This is a bitwise OR of the following flags:

  • wx.MINIMIZE_BOX

  • wx.MAXIMIZE_BOX

  • wx.RESIZE_BORDER

  • wx.SYSTEM_MENU

  • wx.CAPTION

  • wx.CLOSE_BOX

  • wx.CLIP_CHILDREN

wx. MINIMIZE_BOX

Display a title bar button that minimizes the Frame

wx. MAXIMIZE_BOX

Display a title bar button that maximizes the Frame

wx. CLOSE_BOX

Display a title bar button that allows the Frame to be closed. (the "X" button)

wx. RESIZE_BORDER

Allow the Frame to be resized by the user when they drag the border

wx. CAPTION

Displays a caption on the Frame

wx. SYSTEM_MENU

Display a system menu (that is, the menu that is shown when clicking in the frames icon on Windows)

wx. CLIP_CHILDREN

Eliminates flicker caused by the background being repainted (Windows only)