Building Graphical User Interfaces for Python code

When working in R, my work mostly involved reproducible analyses, and consisted mostly of scripts. For the occasional interactive report, Shiny works well for wrapping graphical interfaces around R scripts that can be made into stand-alone applications. For building GUIs in Python, there are more options, including tkinter, wxPython and PyQt.

A graphical batch file renamer

One task that I find easier with a GUI is renaming lots of files, and it turns out to provide a good use case:

  • The operation is pretty simple, to focus mostly on the GUI aspects
  • The various renaming options (prefixing, suffixing, numbering, etc) require different types of widgets.
  • Live preview of the new file names require event-handling

wxpython: old and outdated

wxPython is a GUI library based on the cross-platform wxWidgets library. Setting up wxPython was problematic without using Conda, and although it’s newer than tkinter, the documentation I could find is somewhat spotty, so I decided to skip wxpython altogether.

tkinter: old but workable

The oldest GUI library for Python is tkinter, which usually comes bundled with Python, though you still have to install python3-tk. You can use the Python Automatic GUI Generator (PAGE), or specify the GUI you’re building by hand in Python, which was what I did.

The widgets provided by tkinter are adequate for most purposes. To display the old and new file names in my app, I created two text fields, then populated one filename per line with linebreaks. Hacky, but functional.

Event-handling is accomplished using the widgets’ validation mechanism, which sadly is rather poorly documented. Drag-and-drop support is possible via tkinter.dnd, which apparently is due to be deprecated.

PyQt: modern and slick

The newest interface is PyQt, the Python bindings to Qt, another C++ cross-platform library. Qt also comes with Qt Designer, a graphical layout tool to build your GUI, but it’s certainly possible to create simple applications without it, as you can see in my repository.

Being more modern, PyQt has more widgets, most notably table, which allow a spreadsheet-like layout. I used this to show the old and new file names side-by-side instead of two separate text fields in the tkinter version of the app.

Event-handling is straightforwardly supported for each widget type.

Future plans

A natural extension is to implement regular expression support, since it’s native to many languages. A more advanced feature would be drag-and-drop, which is supported by PyQt.

Written on May 12, 2020