[LinuxFocus-icon]
��ҳ  |  վ����ͼ  |  ����  |  ����

���� | �����ڿ� | ���� | ����LF
This document is available in: English  Castellano  ChineseGB  Deutsch  Francais  Nederlands  Portugues  Turkce  

Hilaire Fernandes
by Hilaire Fernandes
<hilaire(at)ofset.org>

��������:

Hilaire Fernandes �� OFSET �ĸ��ܲ�, OFSET��һ��ΪGnome��Ŀ��ѧ���ƹ㼰����������������֯����д��һ����ΪDr. Geo�Ķ�̬���νṹ������Ŀǰ������������һ��Gnome��Ŀ��ѧ����ѧ����Dr. Genius�Ĺ�����


Ŀ¼:

 

Developing Gnome applications with Python (part 2)

Gnome

ժҪ:

��Щϵ��������Ҫ��Ϊ��Щ��Gnome��GNU/LINUX�����н��г��򿪷��������߶�д�ġ�Ϊʲô��ѡ��Python��Ϊ���������أ���Ҫ����Ϊ������ͨ���ܹ���������ϤPython��������Щ�������ԣ�����˵C���ԣ���Ҫ������Щ������Ҫ�߱���Python�±��̵Ļ�����



 

���蹤�ߣ�

�����������Ѿ���������Щϵ�����µ���ʼ������

������Ҫ:

���ڴ�LibGlade����Python-Gnome�İ�װ���̼�ʹ�÷���ͬ��Ҳ�Ѿ���������Щϵ�����µĿ�ʼ���֡�

 

Drill, �����ṩ����ϰ

��һƪ���µ�Ŀ����Ϊ����ʾ����Gnome��Glade��LibGlade���Լ�Python�����õ����л����·ֱ��ýű�ģʽ�ͽ���ģʽд�ij����IJ�ͬ���ɡ�

ʾ��ʹ����GnomeCanvas�������������Ӳ�������������һ��������չʾ�����Ҹ����������������������������Ǻ����׵ġ�

��������һ���½ڣ�������һ�����ܽṹ�½��У��Ա���˵��Gnome�IJ�ͬ���������ļ��н���һ�����ܽṹ�Ĵ������ɡ��˺������½����������ܽṹ�Ļ����ϼ�����������������������˵���󲿷ֵ�Gnome������

��������ǵ��������ܽṹ���ֽ���Drill(ѵ��)��������Ϊ��ѧ�����е�һ������ƽ̨��Ӧ�������ǵ����Ӽ���ϰ�С���Щ������Ϊ�����IJ�����������ʾ��Щ������ʹ�÷�����

 

��Glade����һ������

������

���ǵ�Ӧ�ó��򴰿�����Glade�������ġ���������ǰ�����������������ĵ�һ��GnomeӦ�ó��򴰿ڣ������Ǹ������������ò�ɾ�����õ�ͼ���Ͳ˵���

Drill����Ҫ���ֱ�GtkPaned��������Ϊ���������ռ䡣������ͼ��

[Drill window]
Fig. 1 - Drill main window

���������ռ䱻��ֱ���֣��м��Ŀɿر߽����������ƶ������Ե���ÿ�������ռ��Ĵ�С�����ߵĹ����ռ���������״�ṹ������GtkTree�����ұߵĹ����ռ���ʱ�ǿյģ��Ժ�����������λ�ø���ʹ���ߵ�Ҫ�����ӷ�֧��

ͨ��Glade�鿴Drill�Ľ����������ܹ���һ���˽����ĸ������ɵĹ��ܡ�������ͼ��

[widgets in tree]
Fig. 2 - tree view of Drill

������ͼ Fig. 2 �п��Կ�����hpanedTree���� (������GtkPaned����)����������һ������, �����ߵ�frame2(������GtkFrame����)�� frame2������exerciceTree���������ܸ��õؽ�һ��GtkFrame��������һ��GtkPaned�����У�ʹ��GTK_SHADOW_IN���͵�shadow��,�����ͱ������ڵ�ס�ɿر߽硣

һ��Gnome�Ի�����About Drill����������������������

[Dialog window]
Fig. 3 - Dialog box "About Drill"

���IJ�ͬ���ֿ���ͨ��Glade�������������������޸ġ�

�����ʹ������������ƣ�

��Python������������Щ����������ʹ�����ǣ�

GnomeӦ�ó��򴰿ڣ�
drillApp

�ɻ��ֵķ�֧����
hpanedTree

��֧��:
exerciceTree

Gnome�ġ����ڡ��Ի��� :
about

��������ͼFig. 2�п�����Щ���������ơ�

�����������򵥵��г��������������ƣ���������Ҫ�˽���������������������Ϣ����������Щϵ�����µ���һ������

�������� �ź� ������ʽ
about clicked gtk_widget_destroy
about close gtk_widget_destroy
about destroy gtk_widget_destroy
button1 (Icon New in the
tool bar)
clicked on_new_activate
new activate on_new_activate
drillApp destroy on_exit_activate
exit activate on_exit_activate
about activate on_about_activate

����������

ͨ��Glade����ָ�������ļ��νṹ�������ǵ������У�������ͨ�����������е�Common Tab��drillApp�Ĵ�С�趨Ϊ400x300,��Ҳ�����趨ˮƽ�ָ��ߵ�λ��Ϊ100������1��

����exerciceTree������������Ϊ��ѡģʽ��һ��ֻ��ѡ��һ����֧����ʵ���ϣ�һ�ν���һ����֧��ѡ�С������������У�ѡ��Selection -> Single����������������ѡ�����Ҫ��

�ƣ���Щ���벻�����ǵ�Drill�����ǽ�����һ���п�ʼ���ǵĿ�����ϰ�����ڣ������ǿ�����Python������Ӧ���������沢����GtkTree������

 

Python����

������Դ����������ƪ�ĵ��������ҵ�������Ҫ������drill.glade������ͬһ��Ŀ¼�¡�

 

����ģ�飨modules��

from gtk import *
from gnome.ui import *
from GDK import *
from libglade import *

 

ʹ����LibGlade����ͼ�ν���

����LibGlade�ⴴ��ͼ�ν��沢�󶨵����������Ĺ����Ѿ����ϸ�������ͬ�������ˣ�����û��Ҫ���ظ���һϸ�ڡ�

��Python���������Ƕ�����һЩȫ�ֱ�����

������״�ṹ��ͨ��Glade�����ģ�����ָ�����������û�ȡ��

exerciceTree = wTree.get_widget ("exerciceTree")

����ͬ����Ҫ��ȡˮƽ������ָ�룬ʵ���ϣ��Ǹ�������GtkPaned�����Կ�����һ���������漰����������ֱ�ֿ���ˮƽ���壬һ�������ߣ�������״�ṹ����һ�����ұߣ�������Щ��֧�����ڣ��������Ƿ���һ����ǩ��

   paned = wTree.get_widget ("hpanedTree")
   label = GtkLabel ("No exercise selected")
   label.show ()
   paned.pack2 (label)

���ǽ���������GTK+�IJο��ֲ� ������GtkLabel��GtkPaned�������Լ�Python�µ�Դ�� /usr/lib/python1.5/site-packages/gtk.py ���԰��������Ⲣ��ȷʹ�ò�����

 

GtkTree����

����Ŀǰ��ƪ���µ��ص㲿�֣�����Ӧ��GtkTree�����е���״�ṹ��

������״�ṹ�������ĵ�����Χ�ƣ�addMathExercices()��addFrenchExercices()��addHistoryExercices()�Լ�addGeographyExercices()��������Щ���������ơ�ÿ����������һ������(����)�ͷ�֧(����)�ı��⣺

def addMathExercices ():
   subtree = addSubtree ("Mathematics")
   addExercice (subtree, "Exercise 1", "Math. Ex1")
   addExercice (subtree, "Exercise 2", "Math. Ex2")

������Subtree��

def addSubtree (name):
   global exerciceTree
   subTree = GtkTree ()
   item = GtkTreeItem (name)
   exerciceTree.append (item)
   item.set_subtree (subTree)
   item.show ()
   item.connect ("select", selectSubtree)
   return subTree

��һ����״�ṹ�д���һ������������Ҫ�������£����ȣ�����һ��GtkTree��״�ṹ��һ��GtkTreeItem�������������ı��⣩�����Σ���GtkTreeItem�������ӵ������ṹ�������������������ϣ�Ȼ��������set_subtree()������GtkTreeItem�������������������󣬽�select�¼��󶨵�GtkTreeItem�������Ӷ�����һ���౻ѡ�е�ʱ����selectSubtree()�����������á�

GtkTreeItem

def addExercice (category, title, idValue):
   item = GtkTreeItem (title)
   item.set_data ("id", idValue)
   category.append (item)
   item.show ()
   item.connect ("select", selectTreeItem)
   item.connect ("deselect", deselectTreeItem)

��Щ����ʹ�÷�֧��������Ϊ�����Լ��ı��⣬�����������Exercice 1��Exercice 2��...ÿ���������Ƕ�����һ�� id �������ԡ�GTK+��������һЩ���Ե��κ�GtkObject���͵IJ����ϣ�ÿ��GTK+����������GtkObject�����������ģ���ʵ������������Ҫ����������set_data (key, value) �� get_data (key) ȥ���ɳ�ʼ���Լ�����һ�����Ե�ֵ�����������ӵ��������ϣ������ϣ��������� show() ��������������ʾ���������󣬰����� select �� deselect �¼�����һ��������ʧѡ�񽹵���ʱ����deselect �¼����������ͬʱ��deselectTreeItem() ���������ã�Ȼ����ѡ�е�������selectTreeItem() �����������á�

 

��������

���Ƕ�����3������������selectTreeItem()��deselectTreeItem() �� selectSubtree()������ʹ�� id ���Ե�ֵ�������ı���ǩ���ұߵģ���

 

��β�

���ǽ���������һ�����������������ӷ�֧�����������¿����IJ�������������Ҫ������GtkTree�����Լ���������Щ�����ϰ������ԡ����ֻ��ƾ������ڴӴ��������л�ȡ������Ϣ�����¸��½ڣ��������Գ��԰Ѹ����������ڵ�һ����ʹ�ù���Couleur��Ϸ��ΪDrill��һ����ϰ��

 

��¼��������Դ��

#!/usr/bin/python
# Drill - Teo Serie
# Copyright Hilaire Fernandes 2001
# Release under the terms of the GPL licence
# You can get a copy of the license at http://www.gnu.org from gtk import *
from gnome.ui import *
from GDK import *
from libglade import * exerciceTree = currentExercice = label = None

def on_about_activate(obj):
    "display the about dialog"
    about = GladeXML ("drill.glade", "about").get_widget ("about")
    about.show ()

def on_new_activate (obj):
    global exerciceTree, currentExercice

def selectTreeItem (item):
    global label
    label.set_text ("L'exercice " +
             item.get_data ("id") + "est sélectionné.")

def deselectTreeItem (item):
    global label
    label.set_text ("L'exercice " +
             item.get_data ("id") + "est désélectionné.")

def selectSubtree (subtree):
    global label
    label.set_text ("No selected exercise")

def addSubtree (name):
    global exerciceTree
    subTree = GtkTree ()
    item = GtkTreeItem (name)
    exerciceTree.append (item)
    item.set_subtree (subTree)
    item.show ()
    item.connect ("select", selectSubtree)
    return subTree

def addExercice (category, title, id):
    item = GtkTreeItem (title)
    item.set_data ("id", id)
    category.append (item)
    item.show ()
    item.connect ("select", selectTreeItem)
    item.connect ("deselect", deselectTreeItem)

def addMathExercices ():
    subtree = addSubtree ("Mathématiques")
    addExercice (subtree, "Exercice 1", "Math. Ex1")
    addExercice (subtree, "Exercice 2", "Math. Ex2")

def addFrenchExercices ():
    subtree = addSubtree ("Français")
    addExercice (subtree, "Exercice 1", "Français Ex1")
    addExercice (subtree, "Exercice 2", "Français Ex2")

def addHistoryExercices ():
    subtree = addSubtree ("Histoire")
    addExercice (subtree, "Exercice 1", "Histoire Ex1")
    addExercice (subtree, "Exercice 2", "Histoire Ex2")

def addGeographyExercices ():
    subtree = addSubtree ("Géographie")
    addExercice (subtree, "Exercice 1", "Géographie Ex1")
    addExercice (subtree, "Exercice 2", "Géographie Ex2")

def initDrill ():
    global exerciceTree, label
    wTree = GladeXML ("drill.glade", "drillApp")
    dic = {"on_about_activate": on_about_activate,
        "on_exit_activate": mainquit,
        "on_new_activate": on_new_activate}
    wTree.signal_autoconnect (dic)
    exerciceTree = wTree.get_widget ("exerciceTree")
    # Temporary until we implement real exercice
    paned = wTree.get_widget ("hpanedTree")
    label = GtkLabel ("No selected exercise")
    label.show ()
    paned.pack2 (label)
    # Free the GladeXML tree
    wTree.destroy ()
    # Add the exercices
    addMathExercices ()
    addFrenchExercices ()
    addHistoryExercices ()
    addGeographyExercices ()

initDrill ()
mainloop ()

 

����ƪ���·�������

ÿƪ���¶��и��Եķ���ҳ�档������ҳ����������ύ���ۣ�Ҳ���Բ鿴�������ߵ����ۣ�
 ����ҳ�� 

��ҳ��LinuxFocus�༭��ά��
© Hilaire Fernandes, FDL
LinuxFocus.org

����������LinuxFocus������������������
������Ϣ:
fr --> -- : Hilaire Fernandes <hilaire(at)ofset.org>
fr --> de: Günther Socher <gsocher(at)web.de>
de --> en: Guido Socher <guido(at)linuxfocus.org>
en --> zh: Free Neil <freeneil(at)sohu.com>

2000-01-05, generated by lfparser version 2.23