Saturday, May 31, 2008

PyGTK and Glade on Windows

It's been told that the PyGTK & Glade is a cross-platform developing combo. And there's a good reason to believe that most folks started it from Linux as I did. Well, now it is time to deliver the combo's platform-crossing feature: let's march on to MS Windows. I tested this on Windows XP and so far everything seemed fine. To set up the developing environment, several packages are needed. I list their names as well as downloading links below. Assuming you haven't installed any of them before, I would suggest that you download them all (don't install yet). Even if you have, the version might not be guaranteed to work with other components. I also need to mention py2exe here. It is not essential for the application development. What it does is "compiling" your script into a executable and collecting other related files required by the script together, so that it will be able to run independently on those windows systems where the packages don't exist. Since you can't expect your users to have installed every packages, it indeed roles significantly in this game. The order of the installation does matter here. First you need to install Gtk+ 2.12.9 Development Environment, this includes GTK+ development environment, libglade and Glade3. However the launcher it creates on your desktop has a little problem: it points to glade-2. You can fix that manually by modify its properties. Since many tutorials about PyGTK & Glade only apply on glade2, you may want to install glade-2. You can decompress the package and copy the files into corresponding path (e.g GTK/bin and GTK/share) if you like. Next, install Python 2.5. And then the rest. This is where the order matters. Because the rest of the packages wouldn't be able to detect the right location to go if python was absent. Now the installation is done. You can open IDLE and try import pygtk, gtk and py2exe to see if everything works. You also should also be able to edit you GUI with Glade now. Some more words on py2exe. You can find a brief tutorial on its usage [here]. And how to combine it with PyGTK [here]. The second link tells you to copy some stuff from GTK to your executable path, that makes the size of you application way more bigger that it was. Try to remove some of the unrelated files in these folders, like Docs, as long as your executable behaves properly.

Thursday, May 29, 2008

Creating a message box dialog with PyGTK and Glade

Being a newbie to Python, GTK and Glade, I found it rather enjoyable developing stuff fast and easily with this language/library/tool combination. All you need to do to get a GUI up and running is to "draw" it with Glade and connect it with pygtk.

What makes it not so perfect is that there's hardly any systematical material online to tell folks how these great things works together. I could only find some separate pieces of information here and there about this, as I wrote before. Fortunately these pieces information are all great. So I highly recommended those absolute beginners to read this essay, to say the least, to get things started.

Here I'm going to show the way to create a message box, which is probably the most (simple?) commonly used dialog in a GUI. It has a OK button and displays a message you passed. Like this:

When you use it, simply create a instance and pass the message (and the title of the dialog if necessary) to it:

1 from msgBox import messageBox
2 messageBox('Guess what? I am a message box!')


How does that look?

First of all, let's draw the message box out. This is really easy:

Open Glade2, find "dialog" in the palette and click it. Choose "Standard Button Layout", "OK". After the dialog shows up, click "label" on the pallette and then click on the blank area in the previous dialog window.

Next, we are going to set up the signals. Thanks to glade, this part becomes so simple in our case that all we need is click the OK button (or, notice that you can always go to View->Show Widget Tree to focus on the widget you are looking for). In the Properties window, choose "Signals" tab, click on the "..." button after "Signal" entry. Find "clicked" and click "OK". Now don't forget to click "Add" afterwards.

Notice that here we are using mostly default names: dialog1,okbutton1,label1,etc. And you can always choose your own.

Now save the project and quit, I name it "msgBox". And you should get a new folder named the same as the project and two files with the name plus suffix .glade and .gladep. If you try to open them with a editor, you will find them just XML files that represent the widget trees and the properties of each widget we created.

Now type in the following script in a editor and save the file as msgBox.py (line number not included, of course), I'll explain it line by line.
msgBox.py

 1 try:
 2         import pygtk
 3         pygtk.require('2.0')
 4         import gtk
 5         import gtk.glade
 6 except:
 7         print 'Install pygtk,libgtk2.0 and libglade2.0'
 8         import os
 9         os.exit(1)
10
11 class messageBox:
12         def __init__(self, lbl_msg = 'Message here',
13                         dlg_title = ''):
14                 self.wTree = gtk.glade.XML('msgbox.glade')
15                 self.dlg = self.wTree.get_widget('dialog1')
16                 self.lbl = self.wTree.get_widget('label1')
17                 self.dlg.set_title(dlg_title)
18                 self.lbl.set_text(lbl_msg)
19                 handlers = { 'on_okbutton1_clicked':self.done }
20                 self.wTree.signal_autoconnect( handlers )
21
22         def done(self,w):
23                 self.dlg.destroy()

Line1-9 imports the modules we need. If you've checked the recommended essays in the beginning, you shouldn't have problems with these lines.

The rest of the code defines a class that calls up our message box dialog.

Line 12-13: We pass two arguments to the __init__ method: the message you are going to display and the title of the the dialog window.

Line 14: this is the highlight of the whole point. The method gtk.glade.XML takes the XML file we created with glade, parses it and creats gtk.glade.XML object( which has the widgets tree and some methods ) accordingly.

Line 15-16 uses the gtk.glade.XML method get_widget to fetch the references of the widgets, whose names are passed as arguments.

Line 17, as you may guess, sets the title of the dialog window to the name we assigned.

Line 18 sets the message.

Line 19-20: remember the signal we set for the OK button? here we bind the signal and its handler in a dictionary and pass it to the method gtk.glade.XML.signal_autoconnect. That means when the GUI releases the signal "on_okbutton1_clicked" (which happens when the OK button is clicked, obviously), the program will call the corresponding method( self.done ).

Line 22-23: defines the method done, which is our handler of the signal "on_onkbutton_clicked". It does one thing: call the destroy method, which will terminates the dialog.

A little summary here: we created the GUI object according to the XML files, set up the message we intended to play and make the "OK" button destroys the dialog.


Now you get msgBox.glade, msgBox.gladep and msgBox.py. Whenever you need a message box in your application, just copy them to your package and take advantage of it as introduced in the beginning. The following is a package of all the files described before as well as a simple example of using the class:

http://code-of-danmarner.googlecode.com/files/msgBox.zip

Monday, May 26, 2008

Resources to learn PyGTK ( Python ) and Glade

I've developed a good interest in the combination of Python and Glade. And it did give me some exciting moments creating GUIs so easily and quickly. I am going to write about it as soon as I feel comfortable playing around this (update: here they are!). Before that, here's a bunch of useful links that I found while digging into it. Great Documentation/Tutorials/Essays on PyGTK's official website: http://www.pygtk.org/ Documentation on Glade's website: http://glade.gnome.org/documentation.html Awesome essays on some awesome blogs: Creating a GUI using PyGTK and Glade Building an Application with PyGTK and Glade A Beginner's Guide to Using pyGTK and Glade A walkthough on how to get glade work on Win32: http://faq.pygtk.org/index.py?req=show&file=faq21.002.htp

Monday, May 19, 2008

A Python illustration of how abstract superclass works

I made this after reading Learning Python 3rd Edtion, Part VI, Page 490.

The picture shows what happens when a instance's method, which is declared in a superclass and implemented later in a subclass, is called.

Wednesday, May 14, 2008

Starcraft Brood War on Ubuntu Hardy Heron ( 8.04 )

Since the message that Starcraft II is coming out gets around a lot these days, I start to recall all the good time when I was in crazy about Starcraft. And I did a little more than recalling about it. I installed it on Ubuntu Linux. Now I can click on the Starcraft icon and play the newest version of the game anytime I like on my laptop, UNDER LINUX! My laptop is a Gateway T1616 ( Specifications ). The OS is Ubuntu 8.04 (Hardy Heron). Here's something you need to do in order to get this: First of all, get your Starcraft disk images prepared. If you have the CD,put it in your first cdrom and type these in a terminal:
sudo umount /dev/cdrom0 dd if=/dev/cdrom0 of=/your_path/Starcraft_Original.iso
If you also have the BroodWar CD, go ahead and do the same thing, using a different name for the image file, for instance,Starcraft_Broodwar.iso Or, you can get the iso images in other ways. You may also need the update patch, which can be found here. Here's what you should get after all, to say the least:
BW-1152.exe Starcraft_Original.iso Starcraft_Broodwar.iso
Install wine. This is easy for Ubuntu users:
sudo aptitude update sudo aptitude upgrade sudo aptitude install wine
Install Starcraft. You need to find a place to mount the iso files, I did this:
sudo mkdir /mnt/starcraft
Mount the original image:
sudo mount -o loop /your_path/Starcraft_Original.iso /mnt/starcraft
Install the original version:
wine /mnt/starcraft/install.exe
Of course you need a CD-Key here. I suggest that you install it with the default settings. Then, mount the Broodwar image, and install it:
sudo mount -o loop /your_path/Starcraft_Broodwar.iso /mnt/starcraft wine /mnt/starcraft/install.exe
Update the game to the newest version:
wine /your_path/BW-1152.exe
The installation of the game is easy enough to be skipped, I think. You should be able to find the game under Start-Wine-Programs-Starcraft now. However, it requires the CD. Start playing the game. Add the path you mount your image files as a device in wine:
cd ~/.wine/dosdevices/ ln -s /mnt/starcraft d:
Here, I hadn't added any wine devices so I used 'd:'. Mount the image file (if it is not mounted yet):
sudo mount -o loop /your_path/Starcraft_Broodwar.iso /mnt/starcraft
And then enjoy the game! Well, this is almost the end of the story. At last here's something for those who don't want to do the mounting work every time they want to play. Edit /etc/fstab , so that the image will be mounted automatically when the OS starts. Add this in a new line:
/your_path/Starcraft_Broodwar.iso /mnt/starcraft iso9660 loop 0 0

Monday, May 12, 2008

Downloading all links on a web page in Linux

In a terminal:
$wget -np -r -P path URL
path is the path where you want everything to be saved, while URL is the web page from which you want to download stuff as well as itself.

Wednesday, May 7, 2008

Converting other encodings into Unicode with Python

Recently I need to convert some files encoded with gb2312 into utf-8, here's the way to achieve this in Python:
# Suppose gb2312_line is a string encoded in gb2312,utf8_line is the converting result. utf8_line = gb2312_line.decode('gb2312').encode('utf8')

Monday, May 5, 2008

Installing Ubuntu 8.04 (Hardy Heron) From Hard Disk

This is a way to install Ubuntu without burning a installation CD. Download the alternative desktop CD you need(x86 or amd64) from the ubuntu website. Prepare a swap partition(if you haven't had one), a/some partition(s) for the new installation. A good tool to do this is GParted. Next thing to do is to download two special files for ubuntu hard disk installation: initrd.gz and vmlinuz. From x86: http://archive.ubuntu.com/ubuntu/dists/hardy/main/installer-i386/current/images/hd-media/ amd64: http://archive.ubuntu.com/ubuntu/dists/hardy/main/installer-amd64/current/images/hd-media/ Make sure the file names are initrd.gz and vmlinuz. Firefox added a .htm extension automatically on vmlinuz when I downloaded it. Place the two files as well as the iso file in a partition which is different from the one you intend to install the new system. Attention: this is very import, or the installation will fail because it tries to remove the needed files during the process. For example, you want to install ubuntu on sda8, and you store the files mentioned above in sda7, under /path/ . If you are not familiar with the partition naming convention for linux, you may want to read this first. Make sure you have a working GRUB. It could be the one you are using to boot existing operating systems, or one on a live CD. For windows users, get a grub4dos Reboot the box, when GRUB shows up to let you choose which system to boot, press c. You will find yourself in command line with a leading
grub>
For those who don't know, grub counts hard disks and partitions from 0, so the third partition on the second hard disk is (hd1,2). Type in the command as follows (under the previous path assumption):
root (hd0,6) kernel (hd0,6)/path/vmlinuz initrd (hd0,6)/path/initrd.gz boot
Then you will forward into the normal installation process. And that is the end of this story. Enjoy your Hardy Heron!