Simple Hello World Program In Python Using Glade For The GUI

Linux tutorials and code.
3 posts Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

Today will be using Glade Interface Designer to create our graphical user interface. (Glade is cross platform, and will run on windows, linux, and mac os x) In this project Glade will be doing pretty much all the work for our program, but we will need to incorporate some code in Python so lets begin.

First off download Glade via Terminal, Tarball, Add Remove Section, etc:

For Ubuntu, Kubuntu, Xubuntu, Mythbuntu, and Debian users we can install Glade using the terminal by using the following terminal command. (Do not close your terminal after installing glade as we will be using it later on to finish up our program, so just minimize it for now)
Code: Select all
sudo apt-get install glade
Now that you have Glade installed go ahead, and open it.
You will then see a window pop up with the title saying "Unsaved 1 preferences" just press the close button, as we don't need to change anything there today.

Now add click on the window button to add a window (by the top left, which is highlighted in green)

Now on the right change the name of the window title of the program to "Hello World!"

Below you'll see "Default Width" and "Default Height" check the boxes and you can give your program custom dimensions. (I chose 320x240)
1.png
Click on the Common tab, and go down to where you see "Visible" and click the button to change that to "Yes". Then clock the Signals tab and open GtkObject, and for our destory signal will choose "on_window1_destroy" (without quotes).
2.png
Now click on the label, and then click on the window that was added to add our label.

Now change the text of the label from "label" to "Hello World!".
3.png
Now save the glade project as helloworld.glade

Then open that glade project in a text editor and you should see an XML code similar to this.
Code: Select all
<?xml version="1.0"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.16 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="window1">
    <property name="visible">True</property>
    <property name="default_width">320</property>
    <property name="default_height">240</property>
    <signal name="destroy" handler="on_window1_destroy"/>
    <child>
      <widget class="GtkLabel" id="label1">
        <property name="visible">True</property>
        <property name="label" translatable="yes">Hello World!</property>
      </widget>
    </child>
  </widget>
</glade-interface>
Save that XML code as helloworld.xml as we won't be needing the glade project anymore, cause the XML file here is what creates our GUI now, but we need to incorporate some coding so that way when we close the window the program will actually close, and not run in the background.

Now in our text editor make a new document, and paste the following code in there, and name it helloworld.py
Code: Select all
#!/usr/bin/python

import gtk
import gtk.glade
import gnome.ui

def whateverdestroy(obj):
	gtk.main_quit()

anynamehere = gtk.glade.XML("helloworld.xml")
dic = { "on_window1_destroy" : whateverdestroy }
anynamehere.signal_autoconnect (dic)
gtk.main()
Some of the names you may notice are kind of weird like "whateverdestroy", or "anynamehere".
anynamehere can obviously be changed, that's just the name of the event, and whateverdestroy is the name of the function that loads, and closes the application. The codes inside the function are the codes that load, and close the program, and notice we're incorporating out helloworld.xml file as the gui. Also notice that we put down "on_window1_destory" which is the closing name we gave out window in Glade.

However we need to make our program executable, and there's a couple ways to do that.

First, because I saved my files to my desktop I need to navigate to my desktop by putting the following terminal command down.
Code: Select all
cd Desktop
Now put down the following terminal command to make our python file/program become executable.
Code: Select all
chmod +x helloworld.py
Now the other way to do this is by right clicking the helloworld.py file and selecting Properties.
Then click the Permissions Tab, and then check "Allow existing file as a program".

Were now pretty much done all we have left to do is to is to test our program. (NOTE: Remember all errors will be automatically detected, and will tell you what the error is. As if there's no errors the program will run smoothly)

So now lets put down the following terminal command to test our program.
Code: Select all
./helloworld.py
Our program is now up and running with no errors, but remember right now if you close the terminal the program will close as well.

Image

So anyway that's how you guys can create a program with a GUI in Linux using the Python programming language.
You do not have the required permissions to view the files attached to this post.
Last edited by mikethedj4 on Sun Jun 05, 2011 1:01 pm, edited 5 times in total.
User avatar
bisnes_niko
Serious Programmer
Serious Programmer
Posts: 409
Joined: Tue Aug 24, 2010 1:21 pm

Is it possible to make cross-platform programs with that python?
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

macHard wrote:
Is it possible to make cross-platform programs with that python?
yep
3 posts Page 1 of 1
Return to “Programming”