wxPython Cookbook
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.
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.
See full terms
Do Well. Do Good.
Authors have earned$11,707,309writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
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), EPUB (for phones and tablets) and MOBI (for 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
Top Books
CQRS by Example
Carlos Buenosvinos, Christian Soronellas, and Keyvan Akbary- Leverage your Software Architecture skills by learning everything about CQRS in detail with lots of examples
- Develop faster applications by applying CQRS and fostering Read Models and Projections
- Learn how to apply CQRS into a brownfield project from a pragmatic approach
Recipes for Decoupling
Matthias NobackWrite software that survives
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
C++20 - The Complete Guide
Nicolai M. JosuttisAll new language and library features of C++20 (for those who know previous C++ versions).
The book presents all new language and library features of C++20. Learn how this impacts day-to-day programming, to benefit in practice, to combine new features, and to avoid all new traps.
Buy early, pay less, free updates.
Other books:
Functional Event-Driven Architecture
Gabriel VolpeExplore the event-driven architecture (EDA) in a purely functional way. Learn to design and develop distributed systems that scale. Identify common design patterns in such systems.
Take your functional programming skills to the next level by joining me in developing a distributed system powered by Apache Pulsar and Fs2 streams, all in Scala 3!
The PowerShell Scripting and Toolmaking Book
Don Jones and Jeff HicksLearn the patterns, practices, and details of PowerShell Scripting and Toolmaking from the industry's two most recognized experts on the subject.
Mastering STM32 - Second Edition
Carmine NovielloWith more than 1200 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the first guide around that introduces the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL and STM32CubeIDE development environment.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
On Java 8
Bruce EckelBy the author of "Thinking in Java," covering version 8 of the Java programming language (with updates to Java 17). Intended for “dedicated beginners,” it doesn’t assume you know anything about programming and takes you through the material step-by-step. However, I assume you are able to figure things out. It’s not a “dummies” or “idiot’s” guide.
Top Bundles
- #1
Software Architecture
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #3
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #4
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #6
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #8
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #10
The Python Craftsman
3 Books
The Python Craftsman series comprises The Python Apprentice, The Python Journeyman, and The Python Master. The first book is primarily suitable for programmers with some experience of programming in another language. If you don't have any experience with programming this book may be a bit daunting. You'll be learning not just a programming...