文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>PyGTK, GNOME-python, and Glade Tutorial

PyGTK, GNOME-python, and Glade Tutorial

时间:2010-09-11  来源:bigluo

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 sjbrown.geeky.net. 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 [email protected].

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:
  • project1.glade [1.3k]
  • hw.py [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:
  • project2.glade [11k]
  • gnome-simple.py [924b]

Dynamic User Interface

Here is the reference documentation for GtkNotebook.

Useful Links

The Whole PyGTK FAQ
Robert Laing's Writing Gnome Applications With Glade and Python
John Finlay's PyGTK Tutorial
Hilaire Fernandes' Developing GNOME Application with Python
Daniel Kornhauser's PyGNOME Tutorial

Credits / Thanks

Thank's to SJbrown; without him/her/them, this tutorial would not exist.
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载