Around my wxPython application

Let's assume that you've used wxPython to create a program, and your program is working, and now you'd like a better understanding of exactly how it works. You've seen the PyCrust features listed earlier in this chapter, and they look like they could be very useful in gaining insights into the functioning of your program. But you'd rather not change your program just to be able to use PyCrust. What do you do?

By passing the name of your program to the PyWrap utility, you can start your program with a PyCrust shell wrapped around it, without changing any of your program. Listing 4.2 displays a program, spare.py, that we are preparing to wrap with PyCrust.

Listing 4.2 The spare.py program being prepared for a PyCrust wrapper

#!/usr/bin/env python

Spare.py is a starting point for simple wxPython programs

import wx class Frame(wx.Frame): pass class App(wx.App):

def OnInit(self):

self.frame = Frame(parent=None, id=-1, title='Spare')

self.frame.Show()

self.SetTopWindow(self.frame)

To run this program with PyCrust wrapped around it, pass the name of the program to PyWrap from the directory where spare.py resides. On Linux, the command line looks like this:

$ pywrap spare.py

When started, PyWrap attempts to import the module included in the command line. PyWrap then looks inside that module for a wx.App subclass, and creates an instance of that class. After that, PyWrap creates a wx.py.crust.Crust-Frame window with a shell, exposes the application object to the PyCrust namespace tree, and starts the wxPython event loop.

The complete source code for PyWrap is provided in listing 4.3. This is an example of how a great deal of functionality can be added to your program with just a small amount of additional code

Listing 4.3 PyWrap.py source code

PyWrap is a command line utility that runs a python program with additional runtime tools, such as PyCrust

_author_ = "Patrick K. O'Brien <[email protected]>"

_cvsid_ = "$Id: PyCrust.txt,v 1.15 2005/03/29 23:39:27 robind Exp $"

import os import sys import wx from wx.py.crust import CrustFrame def wrap(app):

wx.InitAllImageHandlers() frame = CrustFrame() frame.SetSize((750, 525)) frame.Show(True)

frame.shell.interp.locals['app'] = app app.MainLoop()

def main(modulename=None):

sys.path.insert(0, os.curdir) if not modulename:

print "Please specify a module name." raise SystemExit modulename = sys.argv[1] if modulename.endswith('.py'): modulename = modulename[:-3] module = __import__(modulename) # Find the App class. App = None d = module._dict_

if issubclass(d[item], wx.App): App = d[item] except (NameError, TypeError): pass if App is None:

print "No App class was found." raise SystemExit app = App() wrap(app)

After running the PyWrap command, both the simple frame from spare.py and a PyCrust frame are displayed.

PyCrust in action

Now let's see what we can do with the spare.py application frame from within the PyCrust shell. Figure 4.10 displays the result. We'll start by importing wx and adding a panel to our frame:

>>> app.frame.panel = wx.Panel(parent=app.frame) >>> app.frame.panel.SetBackgroundColour('White') True

>>> app.frame.panel = wx.Panel(parent=app.frame) >>> app.frame.panel.SetBackgroundColour('White') True

How Remove Title Bar Wxpython
Figure 4.10 Using PyWrap to enhance a wxPython program at runtime

The panel that was added to the frame began with a default silver color, then it was changed to white. However, setting the panel background color doesn't immediately change its appearance. Instead, something needs to trigger an event that causes the panel to repaint itself, using its new background color property. One way to trigger such an event is to ask the panel to refresh itself:

Now a white panel is displayed, and we're one step closer to understanding the details of how wxPython actually works.

Next, let's add a status bar:

>>> app.frame.statusbar = app.frame.CreateStatusBar(number=3) >>> app.frame.statusbar.SetStatusText("Left", 0) >>> app.frame.statusbar.SetStatusText("Center", 1) >>> app.frame.statusbar.SetStatusText("Right", 2)

Notice how the status bar appears within the frame, without changing the outermost dimensions of the frame. Also notice that the text added to each of the three status bar sections appears immediately, and doesn't require a refresh. Now let's add a menu and a menubar:

>>> app.frame.menubar = wx.MenuBar() >>> menu = wx.Menu()

>>> app.frame.menubar.Append(menu, "Primary") True

>>> app.frame.SetMenuBar(app.frame.menubar)

>>> menu.Append(wx.NewId(), "One", "First menu item")

<wx.core.MenuItem; proxy of C++ wxMenultem instance at

_d8 04 3d08_p_wxMenuItem> >>> menu.Append(wx.NewId(), "Two", "Second menu item") <wx.core.MenuItem; proxy of C++ wxMenuItem instance at

As you manipulate your own wxPython objects in the PyCrust shell, be aware of the impact that the changes have on your running program. Try to answer the following questions. When does the menu actually appear within the frame? What menu attributes can you change while the program is running? Can you add more menu items? Can you remove them? Can you disable them? Exploring all your options interactively should help you better understand wxPython, and provide you more confidence when it comes to writing your actual program code.

Now that we've spent most of the chapter discussing PyCrust itself, we're ready to take a walk through the rest of the Py package components.

Was this article helpful?

0 0

Responses

  • norma
    How to remove title bar wxpython?
    8 years ago

Post a comment