wxPython Cookbook

Retired

This book is no longer available for sale.

wxPython Cookbook

About the Book

The wxPython Cookbook is for anyone wanting to learn more about how to use wxPython. It has over 50 recipes that cover various aspects of the wxPython desktop GUI toolkit. Following is a listing of most of the recipes in no particular order:

  • Adding / Removing Widgets Dynamically
  • How to put a background image on a panel
  • Binding Multiple Widgets to the Same Handler
  • Catching Exceptions from Anywhere
  • wxPython's Context Managers
  • Converting wx.DateTime to Python datetime
  • Creating an About Box
  • How to Create a Login Dialog
  • How to Create a "Dark Mode"
  • Generating a Dialog from a Config File
  • How to Disable a Wizard's Next Button
  • How to Use Drag and Drop
  • How to Drag and Drop a File From Your App to the OS
  • How to Edit Your GUI Interactively Using reload()
  • How to Embed an Image in the Title Bar
  • Extracting XML from the RichTextCtrl
  • How to Fade-in a Frame / Dialog
  • How to Fire Multiple Event Handlers
  • Making your Frame Maximize or Full Screen
  • Using wx.Frame Styles
  • Get the Event Name Instead of an Integer
  • How to Get Children Widgets from a Sizer
  • How to Use the Clipboard
  • Catching Key and Char Events
  • Learning How Focus Works in wxPython
  • Making Your Text Flash
  • Minimizing to System Tray
  • Using ObjectListView instead of ListCtrl
  • Making a Panel Self-Destruct
  • How to Switch Between Panels
  • wxPython: Using PyDispatcher instead of Pubsub
  • Creating Graphs with PyPlot
  • Redirect Python's Logging Module to a TextCtrl
  • Redirecting stdout / stderr
  • Resetting the Background Color
  • Saving Data to a Config File
  • How to Take a Screenshot of Your wxPython App and Print it
  • Creating a Simple Notebook
  • Ensuring Only One Instance Per Frame
  • Storing Objects in ComboBox or ListBox Widgets
  • Syncing Scrolling Between Two Grids
  • Creating Taskbar Icons
  • A wx.Timer Tutorial
  • How to Update a Progress Bar from a Thread
  • Updating Your Application with Esky
  • Creating a URL Shortener
  • Using Threads in wxPython
  • How to Create a Grid in XRC
  • An Introduction to XRC

This book is for wxPython 3.0 and Phoenix. It is also targeted at Python 2 and Python 3 and is written for developers who already have experience with Python and wxPython. This book does not contain an introduction to wxPython or Python.

About the Author

Michael Driscoll
Michael Driscoll

Michael has been programming with Python since 2006. He is the author of the popular Python blog, The Mouse Vs. the Python. Michael has been a technical reviewer for Packt Publishing and No Starch Press.  He is also a contributor on Real Python.

He also co-authored The Essential Core Python Cheat Sheet for DZone.

Michael released his first book, Python 101, June 3rd, 2014. He wrote the follow up, Python 201: Intermediate Python and published it in the summer of 2016. He is also the author of wxPython Cookbook from Apress as well as Python Interviews from Packt Publishing.

Michael is currently working on some new books as well.

Table of Contents

  • Introduction
    • Who Should Read This Book
    • About the Author
    • Conventions
    • Requirements
    • Book Source Code
    • Reader Feedback
    • Errata
  • Installing wxPython
    • Classic
    • wxPython Phoenix
    • The wxPython Docs and Demo Package
    • Wrapping Up
  • Recipe #1: Adding / Removing Widgets Dynamically
    • Wrapping Up
  • Recipe #2: How to Take a Screenshot of Your wxPython App
    • Getting Started
    • The Snapshot Printer Script
    • Wrapping Up
  • Recipe #3: How to Embed an Image in the Title Bar
    • Extracting an Image from the Executable
    • Using Your Own Image
    • Create the Image in Python Code
    • Wrapping Up
  • Recipe #4: How to Put a Background Image on a Panel
    • A Bad Example
    • A Better Example
    • Wrapping Up
  • Recipe #5: Resetting the Background Color
    • Wrapping Up
  • Recipe #6: How to Create a “Dark Mode”
    • Into the Darkness
    • Trying Out Dark Mode
    • Wrapping Up
  • Recipe #7: An Intro to Pubsub
    • The Current pubsub API
    • Wrapping Up
  • Recipe #8: wxPython: Using PyDispatcher Instead of Pubsub
    • Getting Started
    • Wrapping Up
  • Recipe #9: How to Disable a Wizard’s Next Button
    • How do you Disable the Wizard’s Next Button Anyway?
    • Getting it to Work with Phoenix
    • Wrapping Up
  • Recipe #10: Catching Exceptions from Anywhere
    • Catching Exceptions
    • Creating an Exception Catching Decorator
    • Wrapping Up
  • Recipe #11: wxPython’s Context Managers
    • Creating Your Own wxPython Context Manager
    • wxPython’s Context Managers
    • Wrapping Up
  • Recipe #12: Converting wx.DateTime to Python datetime
    • Wrapping Up
  • Recipe #13: Creating an About Box
    • Using wx.AboutBox
    • Using HtmlWindow for an About Box
    • Updating the Code for wxPython Phoenix
    • Wrapping Up
  • Recipe #14: How to Create a Login Dialog
    • Using Pubsub
    • Using an Instance Variable
    • Wrapping Up
  • Recipe #15: Generating a Dialog from a Config File
    • The Config File
    • Creating the GUI
    • Wrapping Up
  • Recipe #16: Saving Data to a Config File
    • Getting Started
    • Creating a Controller
    • Creating the View
    • Wrapping Up
  • Recipe #17: Binding Multiple Widgets to the Same Handler
    • Getting Started
    • Wrapping up
  • Recipe #18: How to Fire Multiple Event Handlers
    • Wrapping Up
  • Recipe #19: Get the Event Name Instead of an Integer
    • Wrapping Up
  • Recipe #20: Catching Key and Char Events
    • Catching Key Events
    • Catching Char Events
    • Wrapping Up
  • Recipe #21: How to Use Drag and Drop
    • Getting Started
    • Creating a FileDropTarget
    • Creating a TextDropTarget
    • Custom DnD with PyDropTarget
    • Creating A Custom Drag-and-Drop App
    • Wrapping Up
  • Recipe #22: How to Drag and Drop a File From Your App to the OS
    • Getting Started
    • Wrapping Up
  • Recipe #23: How to Edit Your GUI Interactively Using reload()
    • Creating the Reloading App
    • Creating the Dynamic Application
    • Wrapping Up
  • Recipe #24: Extracting XML from the RichTextCtrl
    • Extracting XML
    • Updating for Phoenix
    • Wrapping Up
  • Recipe #25: How to Fade-in a Frame / Dialog
    • Wrapping Up
  • Recipe #26: Making Your Text Flash
    • Creating Flashing Text
    • Creating Changing Text
    • Wrapping Up
  • Recipe #27: Making your Frame Maximize or Full Screen
    • Maximizing the Frame
    • Making Your Application Full Screen
    • Wrapping Up
  • Recipe #28: Using wx.Frame Styles
    • Getting Started
    • Create a Frame Without a Caption
    • Create a Frame With a Disabled Close Button
    • Create a Frame Without Maximize/Minimize
    • Create a Un-Resizable Frame
    • Create a Frame Without a System Menu
    • Create a Frame That Stays on Top
    • Wrapping Up
  • Recipe #29: Creating Taskbar Icons
    • Creating the TaskBarIcon in Classic
    • Creating the TaskBarIcon in Phoenix
    • Wrapping Up
  • Recipe #30: Minimizing to System Tray
    • Creating a Simple TaskBarIcon
    • Making the Application Minimize to Tray
    • Wrapping Up
  • Recipe #31: How to Get Children Widgets from a Sizer
    • Wrapping Up
  • Recipe #32: How to Use the Clipboard
    • Wrapping Up
  • Recipe #33: Learning How Focus Works in wxPython
    • Acquiring Focus
    • Losing Focus
    • Wrapping Up
  • Recipe #34: Creating a URL Shortener
    • Creating a Simple Shortener
    • Shortening URLs with Other Shorteners
    • Wrapping Up
  • Recipe #35: Using ObjectListView instead of ListCtrl
    • Wrapping Up
  • Recipe #36: Making a Panel Self-Destruct
    • Wrapping Up
  • Recipe #37: How to Switch Between Panels
    • Wrapping Up
  • Recipe #38: Creating Graphs with PyPlot
    • Getting Started (with a Bar Graph!)
    • Graphing Using Saved Data
    • Point Plot with Thousands of Points
    • Creating a Sine / Cosine Graph
    • Wrapping Up
  • Recipe #39: Redirect Python’s Logging Module to a TextCtrl
    • Wrapping Up
  • Recipe #40: Redirecting stdout / stderr
    • The thread-safe method
    • The Non Thread-safe method
    • Wrapping Up
  • Recipe #41: Creating a Simple Notebook
    • The Simple Notebook
    • The Refactored Notebook
    • Wrapping Up
  • Recipe #42: Ensuring Only One Instance Per Frame
    • Wrapping Up
  • Recipe #43: Storing Objects in ComboBox or ListBox Widgets
    • Adding Objects to wx.ListBox
    • Adding Objects to the wx.ComboBox
    • Wrapping Up
  • Recipe #44: Syncing Scrolling Between Two Grids
    • Wrapping Up
  • Recipe #45: A wx.Timer Tutorial
    • A Simple Timer
    • Using Multiple Timers
    • Wrapping Up
  • Recipe #46: How to Update a Progress Bar from a Thread
    • Updating the Code for wxPython 3.0.2.0 + Phoenix
    • Wrapping Up
  • Recipe #47: Using Threads in wxPython
    • wxPython’s Thread-safe Methods
    • wxPython, Threading, wx.CallAfter and PubSub
    • Making Threads and Pubsub work in Phoenix
    • wx.PostEvent and Threads
    • Wrapping Up
  • Recipe #48: Updating Your Application with Esky
    • Getting Started
    • Adding Update Code to the Initial Release
    • Preparing the New Release
    • Updating Your Program
    • Wrapping Up
  • Recipe #49: An Introduction to XRC
    • Creating a Login Screen with XRC
    • Creating a Notebook with XRC
    • Adding Controls Outside of XRC
    • Wrapping Up
  • Recipe #50: How to Create a Grid in XRC
    • Wrapping Up
  • Recipe #51: An Introduction to XRCed
    • Getting Started
    • Creating Our First Application
    • Creating Something More Complex
    • Using XRCed to Generate Python Code
    • Wrapping Up
  • Recipe #52: How to Center a Widget
    • Using Faux Spacers
    • Using AddStretchSpacer
    • Centering Without Nesting Sizers
    • Wrapping Up
  • Recipe #53: How to Make Widgets Wrap
    • Using WrapSizer
    • Wrapping Up
  • Recipe #54: How to Get Selected Cells in a Grid
    • Grid Cell Selection
    • Wrapping Up
  • Appendix A - Additional Resources
  • Special Thanks

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub