Welcome to wxPython

This chapter covers

■ Getting started with wxPython

■ Creating a minimum wxPython program

■ Importing wxPython

■ Learning the Python programming language

■ Putting it all together

Here's a simple wxPython program. It creates a window with one text box that displays the position of the mouse pointer. Counting white space, it's about 20 lines long.

Listing 1.1 A working wxPython program in a mere 20 lines

#!/bin/env python import wx class MyFrame(wx.Frame):

wx.Frame._init_(self, None, -1, "My Frame", size=(300, 300))

panel = wx.Panel(self, -1) panel.Bind(wx.EVT_MOTION, self.OnMove) wx.StaticText(panel, -1, "Pos:", pos=(10, 12)) self.posCtrl = wx.TextCtrl(panel, -1, "", pos=(40, 10))

def OnMove(self, event):

pos = event.GetPosition()

self.posCtrl.SetValue("%s, %s" % (pos.x, pos.y))

if __name__ == '__main__': app = wx.PySimpleApp() frame = MyFrame() frame.Show(True) app.MainLoop()

What can we say about the program in listing 1.1? It's very short, for one thing. Admittedly, it doesn't do a whole lot, but still, creating a window, populating it, getting it to respond to mouse events—that's not bad for 20 lines. It's not an exaggeration to say this example could easily be three or four times longer in some, more caffeinated, programming languages. Figure 1.1 shows the running program.

The code sample is quite readable. Even if you don't know the details of Python or wxPython, if you have any experience with interface programming you likely have a sense of what words like Frame, _init_, EVT_MOTION, TextCtrl, and MainLoop mean. The indentation might seem a bit weird if you aren't used to Python (where are all those closing braces, anyway?), and you probably don't know what all the arguments mean (what's with those -1s?), but

Figure 1.1 Our first wxPython program, showing the position of the mouse

you could quite easily come to some rough understanding of the code without much help.

In this book, we'll show you why wxPython is one of the easiest, most powerful ways of building a real graphical user interface (GUI) program that there is. Most toolkits that make the building of the interface itself easier (such as a Visual Basic style tool) don't have an implementation language with the clarity, flexibility, and power of Python. Most of the toolkits that have the functionality of wxPython force you to use a language that is ill-suited to rapid development. You'll find wxPython right in the sweet spot, where you get the maximum bang for your development buck. Even better, wxPython is an open-source project, with both the source code and the binary installations distributed under a license that allows it to be freely used in both commercial and open source development.

By the time you've reached the end of this book, you'll know how to build a state-of-the-art GUI using the wxPython toolkit. You'll be able to create and manipulate common interface elements such as buttons and menus, as well as less common ones such as trees and HTML editors. So there's quite a bit of ground for us to cover. In this chapter, we'll get you started with wxPython, and discuss what wxPython does and why you might choose it for your programming needs.

A good interface allows the user to access the functionality of the application as simply and cleanly as possible, with a stylish look that is attractive to the users. A bad interface can keep users from finding the functionality in the program, and can even cause people to assume that a perfectly working program is malfunctioning. In wxPython, you can create the interface you want with less effort than you'd expect.

Was this article helpful?

0 0

Post a comment