Friday, October 29, 2010

Make a GUI wrapper for your favorite CLI program.

Quick, what is your favorite Posix command line program?  What do you mean you don't have one?

In this tutorial I'll show you how to write quick and dirty Python GUIs to wrap some of those programs you use often, but not often enough to remember all of the arguments.  I will be using the apropos command to demonstrate.


If you try to stay out of the command line interfaces as much as possible it's very understandable, was it -r or -R for recursive?  Oftentimes a gui is very comforting because you can see all of the options displayed very spatially rather than in a long string of confusing arguments.  This spacial layout slows the process down for anyone that really knows what they are doing, but honestly is that you?  I use man pages and --help commands extensively while working with unfamiliar commands, which is slower than a GUI.

Materials:
Procedure:
  1. Download and install Python, wxPython, and wxGlade.  For me that was sudo apt-get install python-wxversion python-wxglade , (Ubuntu 10.10)
  2. Start up wxGlade, and design a gui around your application, for me (apropos) I needed a search box, and a results box, with some fancy labels to point out which was which.
  3. General Layout
    Program Preview



  4. Rather than having the user click a button when they want to search, we will have apropos search each time the text box is changed, this is a waste of system resources, but apropos is fast and I like instantaneous results.  So add a listener to your tiny list box in the Events tab of the Properties box when you select it.  Because you want this event called any time new text is entered put a method name in the EVT_TEXT handler box, this method will be called when new text is entered. 
  5. Adding A Method
  6. Select Application in the Tree, your properties box will now change to the export options.  Choose Python, and add an output path for the generated code. Then press Generate Code.
  7. The Code Generation Box
  8. You can now run your newly created Python program, it will have the shape of an application but no functionality yet.
  9. Open the python file in your favorite text editor, and replace the method that you called in EVT_TEXT with one that you make.  Mine looks like this:
  10. def enter_search_text(self, event): # wxGlade: MainFrame.<event_handler>

    import subprocess
    #Call an os function with each arg a separate list element
    process = subprocess.Popen(["apropos", self.text_ctrl_1.GetValue()], stdout=subprocess.PIPE)
    output, unused_err = process.communicate()
    #clear output
    self.text_ctrl_2.Clear()
    #Add the results to the output text control
    self.text_ctrl_2.AppendText(output)

    event.Skip()
  11. You could have named your text controls in wxGlade, I chose not to due to laziness.
  12. Close the program out and run it, you will now be able to search the man pages for programs and settings available on your system.  If I wanted to find all of the calculators on my system I would type in calculator and apropos would find them.
xcalc looks interesting

Conclusions:
Building a GUI for CLI programs is a great way to learn more about the programs themselves, while making life easier for yourself.  You also get to discover new programs and learn Python GUI programming.

No comments:

Post a Comment