BGS, PyZenity and Spaghetti’s

You may be aware I released BGS, Backup Games Saves, a couple of days ago. It’s a tool that enables quick backup of your saved games and savestates in emulators and native games running on Pandora. You never know when your Pandora’s SD Card is going to crash and be corrupted, therefore this backup system saves the data file on your two card slots to remedy against this problem. Allright, now let’s get to PyZenity, one of the tools I used on this project for the first time.

PyZenity is basically just a Python binding for the Zenity library available in, usually, every Linux distro. Zenity makes it easy to create simple GNOME dialogs without adding code for the window, the buttons and so on. It significantly reduces the time you need to put to create graphical interfaces as long as your menus and choices are relatively simple. For a tool like BGS, which was a command line tool at first, this was the ideal choice for me to use this in order to make a GUI version in a couple of hours. Why use Python for this? Before, I used bash for Zenity scripts but I have to admit the bash syntax, while powerful, is a bit dry and harsh, and I prefer Python whenever I have the choice. I did not know that there were python binding for Zenity. And they work pretty well ! The last version of the library is here (http://brianramos.com/?page_id=38) and I used the 0.1.7 version. Just put the module in the folder where you Python script is, and that’s all you need to make it work. For the Python programmers out there, it’s very straightforward to use. Just declare an import a the beginning of your program:

Import PyZenity

And then the usage is dead simple: This command will create simple text box message with a single button, and stop the execution:

a=PyZenity.InfoMessage('Hey this is a message for the dumb user’)

pyze1

For BGS I used a timeout parameter to ensure the program continues by itself even if you don’t click.

a=PyZenity.InfoMessage('Hey this is a message for the user’, timeout=10)

The obvious other instruction you need to know is the case of yes/no dialogs.

a=PyZenity.Question(“Are you a moron ?")

pyze2

Then you can treat the answer by assessing the value of a (which is True is the answer was yes or False if it was no) :

If a==True: print “That’s what I thought. You are indeed a moron”

You can also do lots of other exotic things under the sun, like asking someone to pick up a folder with :

a=PyZenity.GetDirectory()

pyze3

a will be a list containing the selected directories. By default you can only select one, and therefore a will only select a single item in the list, but you can tell the script to have several ones selected:

a=PyZenity.GetDirectory(multiple=True)

If you want to have a specific folder selected at startup, here’s how to do it:

a=PyZenity.GetDirectory(selected=”/media/mmcblk0p1”)

for example this should show up the folders of the first SD Card (unless your SD Card has a different name) right when the folder dialog opens up. If you want to get text input from the user… it’s also very easy:

a=PyZenity.GetText(text=”Tell me your name, moron”)

pyze4

and you will get the string value stored in a. It’s interesting that you can use this to get a password, and hide the text being entered:

a=PyZenity.GetText(text=”Tell me your credit card security code, moron”,password=True)

pyze5

There’s tons of other things you can do with PyZenity. You can even edit a text file with it!

a=PyZenity.TextInfo(filename=”/media/mmcblk0p1/caca.txt”,editable=True

will open the file caca.txt at the root of your first SD Card and make it editable in a text box. What you get in a is the content of the text box after edition.

If you want to explore the possibilities of PyZenity, I encourage you to have a look at the doc (http://brianramos.com/software/PyZenity/docs/). There are tons of command line programs out there that could benefit from a simple GUI and you could be the one providing this kind of GUI using PyZenity and a bit of Python magic, without having to learn GTK+, WxWidgets, Qt or other interesting but time-consuming GUI frameworks.

Let me know if you have questions!

Oh, the Spaghetti’s? Just what I had after finishing this article. Nothing to do with the rest.

Leave a Reply

4 Comments on "BGS, PyZenity and Spaghetti’s"

avatar
  Subscribe  
newest oldest most voted
Notify of
rohezal
Guest

I love Zenity 🙂

kickass
Guest

zenity (and thus pyzenity) has very limited gui features. it’s actually just a very simple graphical backend to script interaction. i wouldnt go so far as calling it a gui (comparing it with complex toolkits as gtk or qt, which are way more than meets the eye…).
other than that it’s awesome!
great article.