Tuesday, May 21, 2013

Tix, ttk and Tkinter

Tkinter is the graphical package based on Tk that ships standard with most Python builds. But Python 2.7 also has ttk and Tix, which leads to some confusion. Right away this is partially cleared up in the reference for ttk, which describes it as an extension to Tkinter that provides missing widgets. But along comes Tix, which says more or less the same thing. Even more confusing, is that we are encouraged to import ttk as follows:

    >>> from Tkinter import *
    >>> from ttk import *

so that those widgets replaced by ttk will be imported over the older ones in Tkinter.

These include:

WidgetTkinterttk
ButtonXX
CanvasX
CheckbuttonXX
ComboboxX
EntryXX
FrameXX
LabelXX
LabelFrameXX
ListboxX
MenuX
MenubuttonXX
MessageX
NotebookX
OptionMenuX
PanedWindowXX
ProgressbarX
RadiobuttonXX
ScaleXX
ScrollbarXX
SeparatorX
SizegripX
SpinboxXTk8.5.9
TextX
TreeviewX
ToplevelX

I'm not going to include Tix because there are over 40 widgets that are added, but there are two important differences between Tix and ttk.
  • Tix hasn't seen development since 2008 whereas ttk was last upgraded in 2012
  • Tix isn't part of Tk/Tcl - it is a 3rd party add-on.
In general ttk looks newer than Tix. Here are some screen shots of a Tix Meter widget and ttk Progressbar widget.
Tix Meter widget
Tix Meter Widget

ttk Progressbar widget
ttk Progressbar widget

So I think I'm going to stick with ttk imported on top of Tkinter as suggested by Python, and I'll only supplement these widgets with Tix if I absolutely must, since the ttk widgets really do seem better.

Thursday, May 9, 2013

Baby Got Backends

What are backends? I mean matplotlib backends for the nearly ubiquitous scientific graphic Python package that complements NumPy/SciPy so well. You can use several graphics libraries including Qt4, GTK, wx and Tk (which is the default). Here in no order are what these backends look like on Windows 7.

Qt4Agg

This is probably the nicest. It is the only one that has the special added feature of allowing you to set the figure title, axes labels and line and marker type and color. Also it is the only one with normal looking sliders.
matplotlib Qt4Agg backend
Qt4Agg backend
 Here you can see the curves tab of the extra figure options box.
matplotlib Qt4Agg backend curves tab of figure options
Curves tab of the figure options box (Qt4Agg backend)

GTKAgg

The other backends look nearly identical. Here is the GTK backend. The sliders are the same as Tk, but the main figure window has buttons whose edges only light up when you hover over them, giving it a slightly more modern look than the TkAgg backend.
matplotlib GTKAgg backend
GTKAgg backend

TkAgg

Here is the default backend. See what I mean about the buttons; but maybe you like that? I know on mac, Tk looks pretty nice, but on Linux and Windows, I think it looks more primitive than the other backends.

matplotlib TkAgg backend
TkAgg backend

wxAgg

The only noticeable difference between wx and the GTK to me was the extra status bar underneath the toolbar. GTK and Tk both put the status on the right side of the toolbar, which makes better use of space, IMHO. Also the nav buttons in the wx backend were grey not blue, and the favicon's were broken - not matplotlibs squiggly curve icon that the other backends used - but that might just be my computer?
matplotlib wxAgg backend
wxAgg backend

Wednesday, May 8, 2013

I heart Doug Hellmann

The most straightforward way to use the debugger is to run it from the command line, giving it your own program as input so it knows what to run.
from his PyMOTW blog on pdb. FYI: you can replace pdb with ipdb.

    me@mymachine ~/projects $ python -m ipdb myscript.py argv1 argv2 ...

Monday, April 29, 2013

spyderlib split

Spyder is the IDE that comes packaged with Python (x, y), and for a community lead project it is a very impressive achievement. For those migrating from MATLAB, they will appreciate the variable explorer, the built in command window, directory and project browser and help.

If only the debugger was better, but it does link to winpdb, which will be more familiar to most MATLAB users than pdb CLI variants.

The trouble is that it consistently has little annoying bugs, which in some cases I think are really show stoppers unfortunately. So I recommend that if you are going to rely on Spyder, download the Python (x, y) distribution or the new Enthought Canopy distribution, because they are tested and frozen at releases that are reliable.

The latest version of Spyder is 2.1.13.1 and like the version before it, there are at least 2 bugs, which intermittantly effect Windows users. The first is the userconfig.py, which will crash the program as soon as it is started, rendering Spyder useless. The fix is simple, apply the patch in comment #7 from issue 1086. The other bug that is not as bad only affects if you try to start Spyder from a BASH console like msysgit Git Bash  or MSYS. For some bizarre reason the shebang for the Spyder script is the WinPython binary, instead of the traditional:
    #! /usr/bin/env python
So just change it to the traditional python shebang and you can now use the script to start spyder from bash.

Also if you want to use git in Spyder, then apply this patch too, although all it does is start either gitk or git-gui, which is fine and actually better than nothing.

ipdb is soo much nicer than pdb

You use Ipython right? So why not use ipdb, its debugger.

However on Windows you  might have an issue if you call ipdb.set_trace() recursively, where the color coding breaks:
Starting ipdb inside ipython breaks color coding (windows)
The fix may be to comment or alter line 43 in __main__. Or just don't call ipdb.set_trace() in a loop, or from ipython :) .

Saturday, April 27, 2013

Sphinx domains for MATLAB

Sphinx is a Python module that generates beautiful documentation using ReStructuredText.

It is extensible to other "domains" other than Python.

I've made a MATLAB domain that you can install on Bitbucket.

Fork or clone it.
To use it you must have Sphinx and its dependencies. Then install the matlabdomain by cloning my fork of the sphinx-contrib repository from Bitbucket. Then run `python setup.py install` from inside the `matlabdomain` directory. If you run `pip freeze` you sill see it listed but there is a warning that the `dependency_links` are not in an svn repository - this is a known bug. After you've run sphinx-quickstart, edit conf.py to include `sphinxcontrib.matlab` in the list of `extensions` and also add the following line:

    primary_domain = 'mat'

Voila! You can now document MATLAB in Sphinx, as in the example above. Do note the TODO in matlab.py - I have not added autodocs for MATLAB. It wouldn't make sense anyway, since MATLAB function and class definitions do not use ReStructuredText markup for their H1 and help comment lines.

I wrote a post on installing Sphinx with latex support. Also you will need a working make binary.

Tuesday, April 16, 2013

JGit & MATLAB Matrimony

MATLAB is a Java interpreter. See Java Libraries in MATLAB help. Now it just so happens that there is an excellent Java implementation of Git, the most widely used distributed version control system, called JGit. So it seems obvious that the best way to use Git in MATLAB is with JGit, and it turns out this is surprisingly easy, because JGit has a porcelain class.



I have written a ridiculous wrapper to use JGit in MATLAB that you can clone from Github or download from the MATLAB Central File Exchange. Try my wrapper, but then either contribute or branch out and just use the JGit package. It is surprisingly easy.

NOTE: Did I mention that you do not need to install anything! JGit is your Git client. JGit is not a wrapper for C/C++/Perl scripts. It is a pure Java implementation of Git. That really makes this the one of the easiest way to use Git if you are also using MATLAB.

Enjoy!
Fork me on GitHub