Category Archives: wxPython

An article about wxPython, one of the most popular desktop graphical user interface (GUI) toolkits for the Python programming language

ANN: Boomslang XML

I recently decided to start putting together some fun example desktop applications using Python. I’ve been using wxPython to create the cross platform applications. My first one is called Boomslang XML and is a basic XML editor.

The name, Boomslang, comes from a large venomous snake. It’s name basically means “tree snake”, which I thought was appropriate since the user interface uses a tree widget to represent the structure of the XML document.

The current features in Boomslang include the following:

  • Opening / Editing multiple XML files
  • Auto save on edit of the XML
  • Recent file support
  • Some keyboard shortcuts (accelerators)
  • Add new XML nodes or attributes
  • Edit nodes and attributes
  • Delete nodes

Currently this is fairly beta, but I thought other people might find it interesting. I am aware of a couple of issues with it currently, such as the inability to delete attributes or not being able to add an XML node with spaces in it But I will get those fixed soon. In the meantime, feel free to check out the project over on Github.

Note: This project was tested with Python 2 and 3, wxPython 2.9, 3.0, and 4.0 using the lxml package on Windows 7, Xubuntu 16.04 and Mac OSX Sierra.

wxPython: Working with Status Bars

Most applications come with the Status Bar. The status bar is the widget along the bottom of most applications that you use every day. They give you information about what line you’re editing in a text editor or when you last saved. In wxPython, you can add a status bar to your frame by using the wx.StatusBar class. In this article, we will learn all about how to use status bars in wxPython.


No Status Bars

It’s always good to start at the beginning. So we will begin our journey by looking at some sample code that shows what a frame looks like without a status bar:

import wx
 
class MainFrame(wx.Frame):
 
    def __init__(self):
        wx.Frame.__init__(self, None, title='No Statusbars')
 
        panel = wx.Panel(self)
 
        self.Show()
 
if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

When you run this code, you should see something like the following:

Continue reading wxPython: Working with Status Bars

wxPython: Learning about TreeCtrls

The wxPython GUI toolkit comes with many widgets. A common control is a tree widget. wxPython has several different tree widgets, including the regular wx.TreeCtrl, the newer DVC_TreeCtrl and the pure Python variants, CustomTreeCtrl and HyperTreeList. In this article, we will focus on the regular wx.TreeCtrl and learn the basics of how to create and use one.

Creating a Simple Tree

Creating a TreeCtrl is actually quite easy. The wxPython demo has a fairly complex example, so I wasn’t able to use it here. Instead I ended up taking the demo example and stripping it down as much as I could. Here’s the result:

import wx
 
class MyTree(wx.TreeCtrl):
 
    def __init__(self, parent, id, pos, size, style):
        wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
 
 
class TreePanel(wx.Panel):
 
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
 
        self.tree = MyTree(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
                           wx.TR_HAS_BUTTONS)    
 
        self.root = self.tree.AddRoot('Something goes here')
        self.tree.SetPyData(self.root, ('key', 'value'))
        os = self.tree.AppendItem(self.root, 'Operating Systems')
        self.tree.Expand(self.root)
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tree, 0, wx.EXPAND)
        self.SetSizer(sizer)
 
 
class MainFrame(wx.Frame):
 
    def __init__(self):
        wx.Frame.__init__(self, parent=None, title='TreeCtrl Demo')
        panel = TreePanel(self)
        self.Show()
 
 
if __name__ == '__main__':
    app = wx.App(redirect=False)
    frame = MainFrame()
    app.MainLoop()

In this example, we create a subclass of wx.TreeCtrl that doesn’t do anything. Then we create a panel subclass where we instantiate the tree and add a root and sub-item. Finally we create the frame that holds the panel and run the application. You should end up with something that looks similar to the following:

This is a pretty boring example, so let’s make something a bit more interesting. Continue reading wxPython: Learning about TreeCtrls

wxPython Phoenix Alpha Release

The wxPython project made a major announcement over the weekend in releasing an alpha version of the new wxPython “Phoenix” package to the Python Packaging Index (PyPI). wxPython is a major cross-platform desktop graphics user interface toolkit for Python. It wraps wxWidgets and is one of the major competitors to PyQt. All new releases of wxPython will be going to PyPI in the future. You can get a copy directly here:

It should also be noted that wxPython is now being distributed as a Python wheel and a tarball. What this means is that you can now install wxPython with pip:

pip install wxPython

If you want to stay on the bleeding edge and use a daily snapshot build, then you do the following:

pip install --pre --find-links http://wxpython.org/Phoenix/snapshot-builds/ wxPython

I’ve been using the Phoenix version of wxPython for over a year and so far it has worked great! You can read more about the differences between it and Classic here:

wxPython Cookbook Artist Interview: Liza Tretyakova

I always put a lot of thought into the covers of my book. For my first book on wxPython, I thought it would be fun to do a cookbook because I already had a lot of recipes on my blog. So I went with the idea of doing a cookbook. For the cover, my first thought was to have some kind of kitchen scene with mice cooks. Then I decided that was too obvious and decided to go with the idea of an Old West cover with cowboy (or cow mice) cooking at a fire.

I asked Liza Tretyakova, my cover artist for wxPython Cookbook, to do a quick interview about herself. Here is what she had to say:

Can you tell us a little about yourself (hobbies, education, etc):

My name is Liza Tretyakova, I’m a free-lance illustrator currently working in Moscow.

Education:

  • Moscow State University, Faculty of History of Arts
  • BA(Hons) Illustration, University of Plymouth


I work as an illustrator for about 10 years. I love horses and I used to have a horse. Also I’m interested in archery. I like reading and spending a lot of time with my daughter Yara, who is 7 years old.

What motivated you to be an illustrator versus some other profession?

Since I was a child I have been drawing all the time and it just happened that I started to work as an illustrator, it turned into a profession.

What process do you go through when you are creating a new piece of art?

It is different every time, there is no specific “recipe” 🙂

Do you have any advice for someone who wants to be an illustrator?

You should try to draw every day, the more the better.

Do you have anything else you would like to say?

It was a pleasure working with you!

Thanks so much for doing the interview and for agreeing to be my illustrator for my wxPython Cookbook.

You can see more of Liza’s work on Behance.

wxPython Cookbook Writing Update: Beta Version Available

I am happy to announce that I now have all the chapters for my latest book, wxPython Cookbook, ready to be checked out. I still consider the book to be in beta mode as I need to go through each chapter and check them over as much as possible this month, but I am also pretty confident that the book is over 90% complete. Some chapters still need a screenshot or two added and I also plan to add another chapter or two as well.

For those of you who like raw data, there are currently 51 recipes in the book + the introduction and installation chapters. There are over 300 pages of content, which is more than either of my previous books!

I hope to do some polishing this week by adding the missing screenshots and also writing a brand new chapter. I am also hoping to get some of the code examples into Github this week. I do apologize for the delay in getting that done. Life has been really crazy on my end.

You can get early access to the book on Leanpub and Gumroad. You will also receive the final product + updates if you purchase the book from either of those websites. You can also check out the original Kickstarter campaign to learn more about the book.

Thanks again for all your support!

wxcookbook_small

wxPython Cookbook Available for Pre-Order

I am excited to announce that the wxPython Cookbook is now available for Pre-Order. You can get your digital copy on Gumroad or Leanpub now. You can get a sample of the book on Leanpub if you’d like to “try before you buy”.

There will be over 50 recipes in this book. The examples in my book will work with both wxPython 3.0.2 Classic as well as wxPython Phoenix, which is the bleeding edge of wxPython that supports Python 3. If I discover any recipes that do not work with Phoenix, they will be clearly marked or there will be an alternative example given that does work.

wxpython_cookbook_final

Here is a partial listing of the current set of 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

You can read more about the project in my Kickstarter announcement article. Please note that the Kickstarter campaign is over.

Related Posts

wxPython Cookbook – Hard Cover Editions Available

I’ve been toying with the idea of doing a hard cover version of my books for a long time so I thought I might try it out with the Cookbook. I went ahead and ran the numbers using my printer on demand resources and discovered that a regular hard cover isn’t crazy expensive. So I have added a new perk priced at $35 for a hard cover version of the book.

But wait! There’s more! Since the cookbook will have so many screenshots and code examples, it occurred to me that it might be fun to offer a color version of the book. That way you can see the screenshots in full color and you’ll also be able to view the syntax highlighting in the code examples too! Unfortunately, printing in color is crazy expensive. But if you are interested in that option, it is available at $100.

All versions of the book will be signed. If there’s a lot of interest in the black-and-white hard cover, I’ll increase the number of items available for that one.

To get either of these editions or the regular paperback, you will need to pledge on Kickstarter.

wxpython_cookbook_final

wxPython Cookbook Cover Story

I always spend some time thinking about how I want my book’s cover to look. When I was designing the Cookbook’s cover, I thought mostly about food and chefs. I had originally thought I might have some kind of kitchen scene with mice in chef hats and a snake on the mantle. But I wanted to take the idea of cooking and put a twist on it.

Instead of a kitchen, I thought of cowboys herding cattle and how they usually had a cook with them. So I went with that idea, although I didn’t have the herds of animals added to the cover.

To help differentiate the Cookbook from my previous works, I hired a different artist from my previous titles named Liza Tretyakova. You can check out some of her work on Behance or even contact her directly by email (schimmel@inbox.ru) if you happen to need a great artist.

I thought it might be fun for you to see how the cover art evolved as I worked with the artist to get my ideas for the cover turned into reality. Let’s start with the first sketch I got from Liza:

wxpython_cookbook_cover_sketch

Continue reading wxPython Cookbook Cover Story

wxPython Cookbook Sample Chapters

My newest book will be my own home brewed version of a wxPython Cookbook. If you’re interested in learning more about it, then please check out the Kickstarter campaign. The brief synopsis is that it will have a little over 50 recipes in the book and around 300 pages of content.

To help you make an informed decision about whether or not you would like to support the book, I am releasing a few sample chapters. You can download them here as a PDF. Please note that these chapters are in a beta state. I will be updating the vast majority of the book with new screenshots and updated code examples where appropriate as well as various other tweaks and enhancements.