1. Introduction
Welcome to my wxPython recipes book! As with most cookbooks, this one is made up of a series of recipes. Some recipes will be more involved than others, but most of the time, the recipe will be a nice bite-sized chunk of information that only covers three to five pages or so. There are more than 50 recipes in this book. I have compiled them over the last eight years from people who have asked questions on the wxPython mailing list, StackOverflow, or e-mailed me directly.
Normally I would spend a lot of time in the introduction going over each section of the book, but since this book is a series of recipes, it wont actually be split into sections. Instead, the recipes will be grouped where possible. For example, I have a number of XRC-related recipes, so they will be kept together as a single chapter.
The recipes will include screenshots of the interfaces that you will be creating. There will be additional screenshots included if and when we change the code inside a recipe. A good example of this is in the Frame Styles recipe where we try out various flags that affect how wx.Frame is displayed.
Who Should Read This Book
This book is targeted at people who are already familiar with the Python programming language and also have a basic understanding of wxPython. At the very least, it would be helpful if the reader understands event loops and the basics of creating user interfaces (UIs) with another Python UI toolkit, such as Tkinter or PyQt .
About the Author
You may be wondering who I am and why I might be knowledgeable enough about Python to write about it, so I thought Id give you a little information about myself. I started programming in Python in Spring 2006 for a job. My first assignment was to port Windows log-in scripts from Kixtart to Python. My second project was to port VBA code (basically a graphical user interface, or GUI, on top of Microsoft Office products) to Python, which is how I first got started in wxPython. Ive been using Python ever since, doing a variation of back-end programming and desktop front-end UIs. Currently I am writing and maintaining an automated test framework in Python.
I realized that one way for me to remember how to do certain things in Python was to write about them and thats how my Python blog came about: www.blog.pythonlibrary.org/ . As I wrote, I would receive feedback from my readers and I ended up expanding the blog to include tips, tutorials, Python news, and Python book reviews. I work regularly with Packt Publishing as a technical reviewer, which means that I get to try to check for errors in the books before theyre published. I also have written for the Developer Zone (DZone) and i-programmer web sites as well as the Python Software Foundation. In November 2013, DZone published The Essential Core Python Cheat Sheet, which I coauthored. Finally, I have also self-published the following two books:
Python 101 , which came out in June 2014.
Python 201: Intermediate Python , which came out in September 2016
Conventions
As with most technical books, this one includes a few conventions that you need to be aware of. New topics and terminology will be in bold . You will also see some examples that look like the following:
>>> myString = "Welcome to Python!"
```
The >>> is a Python prompt symbol. You will see this in the Python interpreter and in IDLE . Other code examples will be shown in a similar manner, but without the >>> .
Requirements
You will need a working Python 2 or Python 3 installation. Most Linux and Mac machines come with Python already installed; however, they might not have Python in their path. This is rare, but if it happens there are lots of tutorials on the Internet that explain how to add Python to your path for your particular operating system. If you happen to find yourself without Python, you can download a copy from http://python.org/download/ . There are up-to-date installation instructions on the web site, so I wont include any installation instructions in this book for Python itself.
The wxPython toolkit is not included with Python. We will look at how to install it here. You will want to use the latest version of wxPython, which at the time of writing, is version 4. It also based on the Phoenix branch of wxPython instead of Classic. You dont really need to know the differences between these other than Phoenix supports Python 2 and 3 while Classic does not.
To install wxPython 4, you can just use pip:
pip install wxPython
This works great on Windows and Mac. I have noticed that on some versions of Linux, you may see an error or two about missing dependencies, such as webkit. You will need to install the listed dependency and then try installing wxPython again.
Once youre done installing wxPython , we can check to make sure it works with the following script:
class MyFrame(wx.Frame):
""""""
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, size=(500, 200),
title='Version Info')
panel = wx.Panel(self)
py_version = 'Python version: ' + platform.python_version()
wx_version = 'wxPython version: ' + wx.version()
os_version = 'Operating System: ' + platform.platform()
main_sizer = wx.BoxSizer(wx.VERTICAL)
size = (20, -1)
main_sizer.Add(
wx.StaticText(panel, label=py_version), 0, wx.ALL, 5)
main_sizer.Add(
wx.StaticText(panel, label=wx_version), 0, wx.ALL, 5)
main_sizer.Add(
wx.StaticText(panel, label=os_version), 0, wx.ALL, 5)
panel.SetSizer(main_sizer)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
This code should run without error and you will see a simple UI appear on screen.
Any additional requirements will be explained later on in the book.
Book Source Code
The books source code can be found on Github:
https://github.com/driscollis/wxPython_recipes_book_code
Reader Feedback
I welcome feedback about my writings. If youd like to let me know what you thought of the book, you can send comments to the following address:
comments@pythonlibrary.org
Errata
I try my best not to publish errors in my writings, but it happens from time to time. If you happen to see an error in this book, feel free to let me know by e-mailing me at the following:
errata@pythonlibrary.org
Now lets get started!
2. Working with Images