wxPython Stopwatch

This is a very short wxPython script that helps to measure time interval during various testing. You can create a timestamp with a single click. It also displays duration between timestamps.

When clicked on the clock face, a list appears on the right on which timestamp, lap time, and cumulative lap time are displayed.

This list is cleared and disappears when it is clicked. You can control the transparency of the clock using mouse wheel.

(source code)

Data Plotting in wxPython

Using wx.lib.plot

wxPython has its own plotting library, which provides simple way of drawing large number of data on a canvas. It is convenient to use and it is fast. However you have only one axis per canvas and you can plot 2D graphs only.

To draw a line graph like above, create line objects using numpy

  214                 x = np.linspace(0,10,500)
  215                 y = np.sin(x)
  216 
  217                 # create lines
  218                 line1 = wxplot.PolyLine(list(zip(x, np.sin(x))),
  219                         colour='red', width=3, style=wx.PENSTYLE_DOT_DASH)
  220                 line2 = wxplot.PolyLine(list(zip(x, -np.sin(x))),
  221                         colour='blue', width=3, style=wx.PENSTYLE_LONG_DASH)

Then generate a graphics object and render it on the canvas. Here the canvas is implemented on a wxPanel. So you can embed it into any wx.Window object.

    1                # create a graphics
    2                 graphics = wxplot.PlotGraphics([line1, line2])
    3                 self.pnlPlot.Draw(graphics)

Using Matplotlib WXAgg backend

For more professional plot, you can use matplotlib, more specifically matplotlib WXAgg backend, where almost all the matplotlib features are available to wx.Python.  Thus you can create plots like below very easily.

The WXAgg Figure object and the FigureCanvas object are implemented on a wx.Panel as class members.

   40         # mpl figure object
   41         self.figure = Figure()
   42         # mpl canvas object
   43         self.canvas = FigureCanvas(self, -1, self.figure)

The shade plot on the left for example was generated by the code below.

  138                 # clear previous plot
  139                 self.pnlPlot.Clear()
  140                 # acquire new axes
  141                 ax1 = self.pnlPlot.AddSubPlot(121)
  142                 # we need figure object too
  143                 fig = self.pnlPlot.GetFigure()
  144 
  145                 # colormap
  146                 cmap = matplotlib.cm.copper
  147 
  148                 # import LightSource
  149                 from matplotlib.colors import LightSource
  150 
  151                 y,x = np.mgrid[-4:2:200j, -4:2:200j]
  152                 z = 10 * np.cos(x**2 + y**2)
  153                 ls = LightSource(315, 45)
  154 
  155                 rgb = ls.shade(z, cmap)
  156 
  157                 ax1.imshow(rgb, interpolation='bilinear')
  158                 im = ax1.imshow(z, cmap=cmap)
  159                 #im.remove()
  160                 #fig.colorbar(im)
  161                 ax1.set_title('shaded plot')

(source code)

wxPython Highlight Frontend

Highlight is a source code syntax highlighter that convert source code into HTML and other formats with syntax highlighting. It comes with a nice GUI as well as a command-line executable. This program implemented utilizes the command-line version of Highlight as its backend. It exposes only limited number of features of the original program. However you may find it convenient in certain situations.

The program has three main windows: one for the source code, one for the HTML view and one for the HTML code. In the HTML view you can check the result rendered by the HTML engine. In the HTML code page, you can see its actual HTML code. The HTML view window and the HTML code window are embedded in the notebook window as separate pages under the source code window.

The upper window (a text control) and the lower window (a notebook) are two client areas of a wx.SplitterWindow. So you can resize them by moving the sash in the middle.

On the right, you have several options to choose such as source syntax, output type, theme, font and size etc. Output result can be sent to the clipboard either in HTML text or in bitmap image. It can be saved to a disk file as well.

To enter a source, you can use a file dialog by clicking on the file name text control. The source window is a drop target for files. So you can drag and drop a source file to the source window. You can also copy and paste text from other programs, or you can just type in your code in the source window. If the source came from a file, the source syntax is automatically selected by identifying the file extension. Otherwise you have to select it manually.

The image generated by the program is done by capturing the screen shot of the HTML view window. So you can control the size of the image and the contents by moving the sash and the vertical scroll bar.

If you select certain area of the source code, then you will have the output that matches with the particular area.

In Windows, this image capturing works on the primary display but may not on the secondary display if your display setting uses different scaling factors on each display. (source code)

wxPython COM Terminal

PySerial provides a convenient way to handle data transfer over COM ports. It works on both Windows and Linux. This program is written using PySerial and wxPython to create a simple terminal emulator. It is pretty much similar to the example presented by PySerial itself.  However in this case, the UI is built on wx.Panel instead of wx.Frame. And actual reading of serial data is done in a separate thread.
All GUI components are populated on a single wx.Panel. On the left, a wx.TextCtrl is located as a terminal window. On the right, handful controls are added for for basic setup and control of the terminal window and the COM port. The controls on the right side can be hidden or shown at any time. This terminal supports ASCII mode and Hexadecimal mode. You can also save data to a disk file.

Since all the features are implemented on a wx.Panel, it can be embedded into any window including main frame window simply by creating an instance and passing a PySerial object(serial.Serial()). You can find an example on which two such panels are populated to implement a UART sniffer. (source code)