wxPython Cookbook

The wxPython Cookbook is for anyone wanting to learn neat tips and tricks with the wonderful wxPython toolkit. With wxPython, you can create cross-platform desktop user interfaces quite easily. There are over 50 recipes included in this book. This book is targeted for Python 2 and 3.

Note: This book is currently no longer available as it was relicensed to Apress Publishing as wxPython Recipes

Cover art by Liza Tretyakova. You can see more of Liza’s work on Behance.

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