Posts Tagged ‘python’

I’ve been working on enhancing the user interface of my LeMMA app, which is written in Python/Tkinter. This discusses some of my thoughts while researching the current state of the art with regards to using Tkinter.

Why did I choose Tkinter when there are other widget libraries that looks better and works natively? My decision was mainly based on the ease of installation by users, i.e. no installation required. Because Tkinter comes standard with Python on almost all platforms (except on Ubuntu where you must explicitly install the python-tk package). I don’t want my users to have to install any third-party widget libraries. Users should be able to simply unzip and run the app immediately.

(For the record, I’ve used PyGtk and like it, but I still haven’t found an easy way to install GTK on Windows, other than installing other GTK apps like GIMP or pidgin…)

However, Tkinter suffers from the following issues:

  • Dated look and feel: Surprisingly, despite Python’s widespread use in the major Linux distros, Tkinter looks ugliest on Linux. For Gnome-based distros, it appears that the PyGtk bridge is a more popular choice for GUI Python apps.
  • Lack of good documentation: Yes, there is the Tk documentation, but what is mentioned there is not necessarily available in Python. And some options don’t work for some widgets/platforms. For certain platforms, e.g. Mac you have to resort to direct Tk calls to get certain behavior, and even then not everything described works. You can find some good resources at NMT and effbot.org, but they are not complete. It’s no wonder that we often refer to it as Tk ‘folklore’.

And some platform-specific problems I discovered are:

  • On Linux, the menu in the OptionMenu widget won’t display if it is taller than screen height. The menubutton+menu approach works, but it doesn’t scroll so it is still not useable. Mac and Windows implementations use native menus that can scroll.
  • Mac doesn’t support flat relief buttons (well, I use them in LeMMA, so they looked really bad).
  • Tix is currently broken at least on Ubuntu due to version mismatch in packaging (Ubuntu python is compiled with Tk 8.5, i.e. ttk support). Ok, this is not really a valid problem, but no Tix-love until Ubuntu fixes its repositories. What? You mean there isn’t any Python Tix apps that people are complaining are broken in Ubuntu…?

The next-generation Tkinter is ttk (Tiled Tk), which is able to use native widgets in Mac and Windows, however:

  • It is NOT a drop-in replacement for Tk. Current apps won’t be automagically beautified. You will need to explicitly import the ttk module.
  • It’s not just a simple import statement. Only a subset of widget options are supported in ttk, and the style options were removed. Styling in ttk has a different approach. Perhaps this is a better way, but it means that old Tkinter programs wanting a fresh coat of paint need to be re-coded.
  • Even if a simple import works, all subclassed Tk widgets will NOT get the benefit of ttk, so no third-party widget extensions, or you will get a weird ‘mashup’ of different widget look-and-feel.
  • On the plus side, assuming the documentation at the TkDocs website is accurate, the standard extended widgets will be styled eventually in future Python releases (version 2.7?). That’s not the case yet for the current Tkinter/Tk8.5 implementations as far as I know.
  • Widgets on Linux will look somewhat better, but it’s still ugly, partly because there is no one standard native widget engine, unlike Mac and Windows. Hopefully, at least GTK and Qt can be supported in future, as they are what’s used in most modern Linux distros today. I don’t think the built-in theming support in ttk is the correct way ahead (I’m still looking for instructions on how to design and install new themes for use in Tkinter).

We are finally seeing the beginning of real cross-platform GUI development with Tkinter, using native widgets, but there are still quite a few raw edges. For now I’ll use the following approach:

  • Current: Use the Tk 8.4 widgets set, and write my own pure Tk widgets extensions, avoiding the use of all third-party Tk extensions.
  • Near-future: Re-code my app to auto-detect the availability of ttk and continue to work with and without it. A command-line option to force either selection is also recommended, in case ttk doesn’t work for some users.
  • (Far?) future: When ttk is finally mature, stable and widespread, to Use only ttk widgets.

Another consideration is the eventual migration of your app to Python 3. That might be good time to make a clean break between 2.x + Tk8.4 apps and 3.x + Tk8.5/Ttk apps.

Of course, if you just want to write new Python apps for the future and don’t care about the current installed base, by all means go ahead and write in Python 3 (which is already supported in Python 2.6) and ttk. We need good apps to encourage people to switch anyway. 🙂


Read Full Post »

chordcalc.py 0.3

A number of changes for chordcalc 0.3, plus one stupid bug fixed:

  • New: AUTO_REDUCE filter option. If no fingerings can be found, try to progressively omit 5th, omit root, and omit 3rd until fingerings are found. This is useful for instruments with fewer strings playing larger chords.
  • New: NO_OPEN filter option. Reject fingerings with open strings.
  • Fixed: filter options not passed in since version 0.1.
  • Added spaces between strings in fingering, to cater for fingerings with 10th fret and above.
  • Added display of chord structure beside the chord name
  • Added descriptive help text for filter options
  • Restructured core functions to be independent of NOTES, TUNINGS, CHORDTYPE dictionaries (no change to command-line interface)

Read Full Post »

chordcalc.py 0.2

Updated version 0.2 of chordcalc.py available. Changes below:

  • Corrections for some chord definitions
  • Minor changes to error messages
  • Default span values for various instruments
  • Options –position and –span now issue warning and set to default values instead of exiting
  • Option -v now just prints the version and exits. All other options are ignored.
  • Fixed bug with chord name parsing
  • No longer exits when invalid chords are encountered. Proceed to next chord if available.
  • Display the chord notes for each fingering.
  • Chord notes are also displayed for each chord type for –help-chords.

Read Full Post »

chordcalc.py – initial release

I finally wrote that program to generate chord fingerings for any fretted instrument. It’s written in Python and meant to be run at the command-line. It’s quite a pleasure writing this in Python – I was surprised to finish writing the core algorithm in just one morning.

Why another chord generation program when there are so many of them on the web already? Well, a variety of reasons: there doesn’t seem to one in Python yet, I want a command-line version (it’s more efficient in some ways), and I want to be able to fine-tune the algorithms. I can slap on a GUI later. Python’s list manipulation capabilities means that I can support instruments of any number of strings, and in any tuning.

I wrote it on my Ubuntu box, but it should work for any platform that Python runs on. It’s written in Python 2.5. I’m still unsure about moving on to Python 3.0.

Interested folks can download the initial release here: chordcalc.py

Let me know if you find any errors e.g. in the chord definitions.

Read Full Post »

I’ve added a custom file browser and playlist function to my pyGTK MIDI player. The file browser is more lightweight and read files faster than the GTK FileChooser widget on the Nokia Internet tablet. The GUI is nothing fancy but it works.

Download playmus version 0.2

I don’t think I’ll try to determine the MIDI song length. It’s too much trouble to do for a simple player app.

Along the way, I learnt how to use the GTK Tree widgets to do the list boxes, and I think they are one of the more powerful frameworks in the GTK suite (they can do much more than just simple list boxes), but for simple list boxes there’s a lot of setup to do. Perhaps I should start writing higher-level abstractions to make it easier to create simple GUI programs.

Read Full Post »

playmus screenshotWrote my first PyGTK program for my Nokia 770. It’s a simple GUI midi player that uses the PyGame module. It works pretty well, although while testing the app PyGame sometimes couldn’t initialise and the only option is to reboot my device. But my final round testing seems to be quite stable. Python rocks on the Nokia 770!

Download playmus.py

I’m thinking of putting in other stuff like song repeat, fade out, perhaps a progress bar (if I can find out the midi file length, coz PyGame doesn’t have that API) or even a song shuffle feature.

Read Full Post »