Tag Archives: wxPython

Book Contest: Creating GUI Applications with wxPython

Last month, I released a new book entitled Creating GUI Applications with wxPython. In celebration of a successful launch, I have decided to do a little contest.

Cover art for Creating GUI Applications with wxPython

Rules

  • Tweet about the contest and include my handle: @driscollis
  • Send me a direct message on Twitter or via my contact form with a link to your Tweet
  • If you don’t have Twitter, feel free to message me through the website and I’ll enter you anyway

The contest will run starting now until Friday, June 21st @ 11:59 p.m. CST.

Runners up will receive a free copy of the eBook. The grand prize will be a signed paperback copy + the eBook version!

Getting the Correct Notebook Tab Across Platforms in wxPython

I was recently working on a GUI application that had a wx.Notebook in it. When the user changed tabs in the notebook, I wanted the application to do an update based on the newly shown (i.e. selected) tab. I quickly discovered that while it is easy to catch the tab change event, getting the right tab is not as obvious.

This article will walk you through my mistake and show you two solutions to the issue.

Here is an example of what I did originally:

# simple_note.py
 
import random
import wx
 
 
class TabPanel(wx.Panel):
 
    def __init__(self, parent, name):
        """"""
        super().__init__(parent=parent)
        self.name = name
 
        colors = ["red", "blue", "gray", "yellow", "green"]
        self.SetBackgroundColour(random.choice(colors))
 
        btn = wx.Button(self, label="Press Me")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL, 10)
        self.SetSizer(sizer)
 
 
class DemoFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """
 
 
    def __init__(self):
        """Constructor"""
        super().__init__(None, wx.ID_ANY,
                         "Notebook Tutorial",
                         size=(600,400)
                         )
        panel = wx.Panel(self)
 
        self.notebook = wx.Notebook(panel)
        self.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.on_tab_change)
        tabOne = TabPanel(self.notebook, name='Tab 1')
        self.notebook.AddPage(tabOne, "Tab 1")
 
        tabTwo = TabPanel(self.notebook, name='Tab 2')
        self.notebook.AddPage(tabTwo, "Tab 2")
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        self.Layout()
 
        self.Show()
 
    def on_tab_change(self, event):
        # Works on Windows and Linux, but not Mac
        current_page = self.notebook.GetCurrentPage()
        print(current_page.name)
        event.Skip()
 
 
if __name__ == "__main__":
    app = wx.App(False)
    frame = DemoFrame()
    app.MainLoop()

Continue reading Getting the Correct Notebook Tab Across Platforms in wxPython

An Intro to StaticBox and StaticBoxSizers

There are many widgets that are included with the wxPython GUI toolkit. One of them is a fairly handy widget called wx.StaticBox. This widget accepts a string and then will draw a box with the string in the upper left-hand corned of the box. However this only works when you use it in conjunction with wx.StaticBoxSizer.

Here is an example of what one might look like:

Simple wx.StaticBox

Now let’s go ahead and write the code you would use to create the example above: Continue reading An Intro to StaticBox and StaticBoxSizers

Creating GUI Applications with wxPython Now Available

My latest book, Creating GUI Applications with wxPython is now available for purchase.

Cover art for Creating GUI Applications with wxPython

Creating GUI Applications with wxPython is a book that will teach you how to use wxPython to create applications by actually creating several mini-programs. I have found that while learning how the various widgets work in wxPython is valuable, it is even better to learn by creating a simple application that does something useful.

In this book, you will be creating the following applications:

  • A simple image viewer
  • A database viewer
  • A database editor
  • Calculator
  • An Archiving application (tar)
  • PDF Merging application
  • XML Editor
  • File search utility
  • Simple FTP application
  • NASA Image downloader

As you learn how to create these applications, you will also learn how wxPython works. You will go over how wxPython’s event system works, how to use threads in wxPython, make use of sizers and much, much more!

The eBook version is on sale on Leanpub for $14.99 until May 15th. You can also purchase the book on Gumroad, or get the paperback or Kindle version on Amazon.

Creating a GUI Application for NASA’s API with wxPython

Growing up, I have always found the universe and space in general to be exciting. It is fun to dream about what worlds remain unexplored. I also enjoy seeing photos from other worlds or thinking about the vastness of space. What does this have to do with Python though? Well, the National Aeronautics and Space Administration (NASA) has a web API that allows you to search their image library.

You can read all about it on their website.

The NASA website recommends getting an Application Programming Interface (API) key. If you go to that website, the form that you will fill out is nice and short.

Technically, you do not need an API key to make requests against NASA’s services. However they do have rate limiting in place for developers who access their site without an API key. Even with a key, you are limited to a default of 1000 requests per hour. If you go over your allocation, you will be temporarily blocked from making requests. You can contact NASA to request a higher rate limit though.

Interestingly, the documentation doesn’t really say how many requests you can make without an API key.

The API documentation disagrees with NASA’s Image API documentation about which endpoints to hit, which makes working with their website a bit confusing.

For example, you will see the API documentation talking about this URL:

  • https://api.nasa.gov/planetary/apod?api_key=API_KEY_GOES_HERE

But in the Image API documentation, the API root is:

  • https://images-api.nasa.gov

For the purposes of this tutorial, you will be using the latter. Continue reading Creating a GUI Application for NASA’s API with wxPython

wxPython 4 and PubSub

The Publish-Subscribe pattern is pretty common in computer science and very useful too. The wxPython GUI toolkit has had an implementation of it for a very long time in wx.lib.pubsub. This implementation is based on the PyPubSub package. While you could always download PyPubSub and use it directly instead, it was nice to be able to just run wxPython without an additional dependency.

However, as of wxPython 4.0.4, wx.lib.pubsub is now deprecated and will be removed in a future version of wxPython. So now you will need to download PyPubSub or PyDispatcher if you want to use the Publish-Subscribe pattern easily in wxPython.


Installing PyPubSub

You can install PyPubSub using pip.

Here’s how to do it:

pip install pypubsub

PyPubSub should install quite quickly. Once it’s done, let’s find out how to use it! Continue reading wxPython 4 and PubSub

How to Distribute a wxPython Application

Let’s say you finished up a wonderful GUI application using wxPython. How do you share it with the world? This is always the dilemma when you finish an amazing program. Fortunately, there are several ways you can share your code. If you want to share your code with other developers, than Github or a similar website is definitely a good way to do. I won’t be covering using Git or Mercurial here. Instead what you will learn here is how to turn your application into an executable.

By turning your code into an executable, you can allow a user to just download the binary and run it without requiring them to download Python, your source code and your dependencies. All of those things will be bundled up into the executable instead.

There are many tools you can use to generate an executable:

You will be using PyInstaller in this tutorial. The main benefit to using PyInstaller is that it can generate executables for Windows, Mac and Linux. Note that it does not support cross-compiling. What that means is that you cannot run PyInstaller on Linux to create a Windows executable. Instead, PyInstaller will only create an executable for the OS that it is ran on. In other words, if you run PyInstaller on Windows, it will create a Windows executable only. Continue reading How to Distribute a wxPython Application

Creating a Calculator with wxPython

A lot of beginner tutorials start with “Hello World” examples. There are plenty of websites that use a calculator application as a kind of “Hello World” for GUI beginners. Calculators are a good way to learn because they have a set of widgets that you need to lay out in an orderly fashion. They also require a certain amount of logic to make them work correctly. For this calculator, let’s focus on being able to do the following:

  • Addition
  • Subtraction
  • Multiplication
  • Division

I think that supporting these four functions is a great starting place and also give you plenty of room for enhancing the application on your own. Continue reading Creating a Calculator with wxPython

Less than 2 Days to Go on wxPython Book Kickstarter

My latest book, Create GUI Applications with wxPython, is coming along nicely. I just wanted to let my readership know that the Kickstarter for it is coming to a close in a little less than 2 days.

If you’d like to get a copy at a cheaper price than it will be when it is released in May later this year, the Kickstarter is really the way to go. You can check out the current table of contents in this post from last week.

Thanks for your support!

Table of Contents for Creating GUI Applications Book

We are coming into the last week of the Kickstarter and I thought I would give you all a quick update. I finished writing up the chapter on creating a calculator today and got started on chapter 7.

Creating GUI Applications with wxPython

I also wanted to let you know what the current table of contents looks like right now:

  • Chapter 1 – Intro to wxPython
  • Chapter 2 – Creating an Image Viewer
  • Chapter 3 – Enhancing the Image Viewer
  • Chapter 4 – Creating a Database Viewer
  • Chapter 5 – Database Editing with wxPython
  • Chapter 6 – Calculator
  • Chapter 7 – Archiver (tarball creation utility)
  • Chapter 8 – MP3 Tag Editor
  • Chapter 9 – XML Editor
  • Chapter 10 – NASA Image Downloader / Search Tool
  • Chapter 11 – PDF Merger / Splitter

There will also be a chapter on creating executables and installers for your application and a couple of appendixes.

Thanks so much for your support!

Mike