Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1401501
  • 博文数量: 343
  • 博客积分: 13098
  • 博客等级: 上将
  • 技术积分: 2862
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-06 00:35
文章存档

2012年(131)

2011年(31)

2010年(53)

2009年(23)

2008年(62)

2007年(2)

2006年(36)

2005年(5)

分类: Python/Ruby

2010-09-11 23:13:06

PyGTK, GNOME-python, and Glade Tutorial

  • Total size [286k]
  • Files/ [25k]
  • Images/ [249k]
  • Tutorial [12k]

Editors Note:

The original PyGnome/PyGTK/Libglade tutorial is located at . This document follows the same format with the same example programs, but they are upgraded to the GTK+2.0 and associated counterparts. The explanation of each image is a little more verbose, and there are more images showing you what to do. The packages needed for this tutorial are:

  • GTK+2.0
  • Glade
  • Python 2.2
  • gnome-python 1.99.x (1.99.16 used)
  • pygtk 1.99.x (1.99.16 used)
  • pyorbit 1.99.x (1.99.4 used) (for gnome.ui)

PyGnome/PyGTK/Libglade Tutorial

As of Monday, October 13, 08:57:40, this tutorial is complete. Mail any corrections to .

Table Of Contents

  1. Reasons for Using PyGnome/PyGTK/Libglade
  2. Requirements
  3. Hello World
  4. Simple GNOME Application
  5. Useful Links
  6. Credits / Thanks

Reasons for Using PyGnome/PyGTK/Libglade

Part 1 was never actually written. I'm not entirely sure of many reasons myself; its all in careful consideration of development speed vs program speed. However, considering that PyGnome/PyGTK are just bindings around C libraries (extensive ones at that), most code running will actually be in the form of compiled C libraries; not interpreted python code. This combined with the readability, extensibility, and flexibility of python make it a rather attractive development platform for GTK apps.

Requirements

Part 2 was also not really written; but the requirements for the purposes of this tutorial are listed at the top of this document.

Hello World

To begin hello world, start up Glade. When Glade opens, you will (most likely) be faced with 3 empty windows that look roughly like the ones below. In order to start where the original tutorial did, we must first create a new project. Chose to start a GTK+ project when prompted; for the rest of the options, the defaults should suffice.

Now that we have a project, select "GTK+ Basic" in the Palette window, and then click on the window icon to create a new basic gtk window. You should see something like this:

Next, select a label (by clicking on the "A") and "paint" it into the window by clicking on the crosshatched surface. In the Properties window, change the Label value from "label1" to "Hello World":

The important thing to remember from the Properties window is that the name of this label widget is "label1" for when we want to access or change the widget.

Next, we'll need to add a signal for the destroy event of window1. We have to do this because we want to tell the program to exit when we destroy the window. If we don't attach a handler, the program would keep running and we'd have to kill it by hand. Select the window1 widget by right clicking somewhere in window1, then choosing window1 >select. The Properties window should change to show that you are accessing the properties to the window, not the label widget.

Click on the elipsis button next to "signal", and chose "destroy" from the list. Glade will automatically suggest a name for the handler; "on_window1_destroy", which is fine for our purposes. Click the Add button and the signal and handler will be added to the list. When the "destroy" signal arrives, it will call on the "on_window1_destroy" function, which we will later define in python.

Now simply save the project and we're done with the Glade component of Hello World. Go to the directory where you saved the project (where the .glade file is), and create a new python script called "hw.py" (or whatever you prefer). It should contain the following code:

#!/usr/bin/python

# import necessary modules (changed from libglade to gtk.glade)
import gtk
import gtk.glade
import gnome.ui

# this function is called when the window gets destroyed
def DestroyFunction(obj):
	gtk.main_quit() 	# exit the gtk main loop

widgetTree = gtk.glade.XML("project1.glade")    # create a widget tree from a glade
                                                # xml, also shows the window

dic = { "on_window1_destroy" : DestroyFunction }    # maps signal handler to function
                                                    # on line 10, DestroyFunction

widgetTree.signal_autoconnect (dic)       # connects signal handlers defined in
                                          # the glade file to actual Python functions

gtk.main()
		

Now, simply make "hw.py" executable (chmod +x hw.py) and run it. You should see a window that says "Hello World". NOTE: Two copies are shown running; the smaller one to the left is how the program will look when its first run (as label widgets in GTK created by glade by default shrink to the size of their textual contents). The copy on the right is enlarged to show that the title of the window.

Downloads

Download files for this project:
  •  [1.3k]
  •  [797b]

A simple gnome application

To begin, we start up glade again. Create a new project, but this time select "GNOME project" instead of "GTK+ project". I'm not actually sure what the difference is between these, but it would make sense to chose GNOME project.
Now, select "Gnome" in the palette window, and choose a "Gnome Application Window" from the menu instead of a GTK window. It is the top left icon in the palette window. As you can see, this starts us off with a few more bells and whistles than the simple GTK window. We've got a standard menu bar with "File, "edit", "view", "settings", and "help", and we've also got a toolbar with "new", "open", and "save" buttons. In addition, we have a crosshatched area like before and a status bar at the bottom of the program.
Let's put a notebook in our window. From the GTK menu, select "notebook", and paint it into the crosshatched area. We don't need al three pages, so just add 1. (Note: The original tutorial said we'd add more dynamically before, but then did not offer an explanation on how to do it. I'm currently reading the docs linked to at the bottom, and will write up extra parts of the tutorial that deal with this.)
You should now see one notebook tab with a crosshatched area inside of it. Just to keep things simple, we'll add a label to this area by painting the same GTK+ basic label widget we used in our "hello world" program.
Next, add a signal to handle the destruction of app1 just like we did in the "Hello world" example. This finishes off the Glade component stuff for now. Let's make our application slightly more complicated than Hello World. All applications manage/manipulate/display some kind of data: because we're using Python (as in Monty), let's manage some data about Silly Walks. We'll create a simple data structure to hold information about Silly walks.
class SillyWalk:
	def __init__(self, cost, speed, desc):
		self.researchCost = cost
		self.speed        = speed
		self.description  = desc
		
We're going to cheat a little bit and not write Get..() and Set..() functions for this class, seeing as its so simple and none are needed for the purposes of this tutorial. When needed, we'll just access the attributes directly.
One of the things we are going to do is seperate the user interface from the logic of the application. By using thelibglade approach, we're halfway there (our static UI specification is in a seperate file from our code: xxx.glade). To fully realize this goal, we'll take some inspiration from the Model/View/Controller pattern, and create a View class whose only job is to alter what is seen by the user. One of the main advantages of this is that the application can have multiple view classes (ie. GnomeView, KDEView, TextView, SDLView, Win32View, etc.), and use the most appropriate one.
class GnomeAppView:
	def __init__(self):
		self.widgetTree   = None

	def Show(self):
		self.widgetTree = gtk.glade.XML("project2.glade")
		dic = { "on_app1_destroy" : self.Destroy }
		self.widgetTree.signal_autoconnect(dic)

	def Destroy(self, obj):
		gtk.main_quit() #make the program quit
		
This is the only class that knows how to display the user interface and respond to callbacks from it. YOu'll notice a lot of similarity with our previous Hello World script. When Show() is called, the application window displays the application. We've attached the on_app1_destroy signal to the GnomeAppView object's Destroy() method (the default name for a gnome-window is app1, not window1). When Destroy() gets called, the program will terminate.
Now we'll add a class to hold multiple SillyWalks and some commands to start up the program.
#!/usr/bin/python
import gtk
import gnome.ui
import gtk.glade

# necessary for gtk.glade.XML() with gnome widgets
gnome.init("app1", "1.0")

# the following line should get rid of the default-icon message, but doesn't
gnome.ui.gnome_window_icon_set_default_from_file('/usr/share/pixmaps/app.png')


class SillyWalk:
	def __init__(self, cost, speed, desc):
		self.researchCost = cost
		self.speed        = speed
		self.description  = desc

class GnomeAppView:
	def __init__(self):
		self.widgetTree   = None

	def Show(self):
		self.widgetTree = gtk.glade.XML("project2.glade")
		dic = { "on_app1_destroy" : self.Destroy }
		self.widgetTree.signal_autoconnect(dic)

	def Destroy(self, obj):
		gtk.main_quit() #make the program quit


class SillyWalkApp:
	def __init__(self ):
		self.view         = GnomeAppView()
		self.walks        = []

	def Start(self):
		self.view.Show()

myApp = SillyWalkApp()
myApp.Start()
gtk.main()
		
Save this code as gnome-simple.py, make it executable, and run it. You should see something like this. NOTE: I (the editor) am not sure why the libglade warning about GnomeApp and default-icon occurs. I cannot tell you if that is something with the modified code (i am just a beginner, like you probably are) or if its a problem with my version of libglade or what. Your mileage may vary; but I have been unable to get rid of this message.

Downloads

Download files for this project:
  •  [11k]
  •  [924b]

Dynamic User Interface

 is the reference documentation for GtkNotebook.

Useful Links




Hilaire Fernandes' Developing GNOME Application with Python

Credits / Thanks

Thank's to SJbrown; without him/her/them, this tutorial would not exist.
阅读(1295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~