|
![]() |
|
![]() by Hilaire Fernandes <hilaire(at)ofset.org> ¹ØÓÚ×÷Õß: Hilaire Fernandes ÊÇ OFSET µÄ¸±×ܲÃ, OFSETÊÇÒ»¸öΪGnomeÏîÄ¿½Ìѧ×öÍÆ¹ã¼°¿ª·¢Ãâ·ÑÈí¼þµÄ×éÖ¯¡£Ëûд¹ýÒ»¸öÃûΪDr. GeoµÄ¶¯Ì¬¼¸ºÎ½á¹¹Èí¼þ¡£Ä¿Ç°ËûÕýÖÂÁ¦ÓÚÁíÒ»¸öGnomeÏîÄ¿½ÌѧµÄÊýѧÈí¼þDr. GeniusµÄ¹¤×÷¡£ Ŀ¼: |
ÕªÒª:
ÕâЩϵÁÐÎÄÕÂÖ÷ÒªÊÇΪÄÇЩÔÚGnome¼°GNU/LINUX»·¾³ÖнøÐгÌÐò¿ª·¢µÄÈëÃÅÕß¶øÐ´µÄ¡£ÎªÊ²Ã´»áÑ¡ÔñPython×÷Ϊ¿ª·¢ÓïÑÔÄØ£¿Ö÷ÒªÊÇÒòΪÈëÃÅÕßͨ³£Äܹ»¸ü¿ìµØÊìϤPython¶ø²»ÊÇÄÇЩ±àÒëÓïÑÔ£¨±ÈÈç˵CÓïÑÔ£©¡£ÒªÀí½âÕâЩÎÄÕÂÐèÒª¾ß±¸ÔÚPythonϱà³ÌµÄ»ù´¡¡£
ËùÐèµÄÈí¼þÒѾÁÐÔÚÁËÕâЩϵÁÐÎÄÕµĿªÊ¼²¿·Ö¡£
Äú»¹ÐèÒª:
¹ØÓÚ´øLibGlade¿âµÄPython-GnomeµÄ°²×°¹ý³Ì¼°Ê¹Ó÷½·¨Í¬ÑùÒ²ÒѾÁÐÔÚÁËÕâЩϵÁÐÎÄÕµĿªÊ¼²¿·Ö¡£
µÚһƪÎÄÕµÄÄ¿µÄÊÇΪÁËÑÝʾÔÚÓÃGnome¡¢Glade¡¢LibGlade¿âÒÔ¼°PythonËùÅäÖõÄÔËÐл·¾³Ï·ֱðÓýű¾Ä£Ê½ºÍ½»»¥Ä£Ê½Ð´µÄ³ÌÐòµÄ²»Í¬×é³É¡£
ʾÀýʹÓÃÁËGnomeCanvas²¿¼þ¡£Õâ¸öÀý×Ó²»½öÏòÎÒÃÇ×öÁËÒ»¸öÉú¶¯µÄչʾ£¬¶øÇÒ¸æËßÎÒÃÇÔÚÕâÑùµÄÅäÖÃÏÂ×ö¿ª·¢ÊǺÜÈÝÒ׵ġ£
ÔÚËæºóµÄÒ»¸öÕ½ڣ¬½¨ÒéÔÚÒ»¸ö¿ò¼Ü½á¹¹Ï½øÐУ¬ÒÔ±ãÓÚ˵Ã÷GnomeµÄ²»Í¬²¿¼þ¡£±¾Îļ¯Öн²ÊöÒ»¸ö¿ò¼Ü½á¹¹µÄ´´½¨Éú³É¡£´ËºóµÄÎÄÕ½«ÔÚÕâ¸ö¿ò¼Ü½á¹¹µÄ»ù´¡ÉϼÓÈë¸ü¶àµÄÌØÕ÷²¿¼þÀ´¾ÙÀý˵Ã÷´ó²¿·ÖµÄGnome²¿¼þ¡£
ÔÚÕâÀÎÒÃǵÄÕâ¸ö¿ò¼Ü½á¹¹Ãû×Ö½Ð×öDrill(ѵÁ·)¡£Ëü½«×÷Ϊ½Ìѧ¹ý³ÌÖеÄÒ»¸ö»ù´¡Æ½Ì¨£¬Ó¦ÓÃÔÚÎÒÃǵÄÀý×Ó¼°Á·Ï°ÖС£ÕâЩÀý×Ó×÷Ϊ¶ÀÁ¢µÄ²¿·ÖÏòÎÒÃÇÑÝʾÄÇЩ²¿¼þµÄʹÓ÷½·¨¡£
²¿¼þ£º
ÎÒÃǵÄÓ¦ÓóÌÐò´°¿ÚÊÇÓÃGladeÀ´´´½¨µÄ¡£ÀàËÆÓÚÔÚÇ°ÃæµÄÎÄÕÂÖÐÄú´´½¨µÄµÚÒ»¸öGnomeÓ¦ÓóÌÐò´°¿Ú£¬µ«ÔÚÄǸö´°¿ÚÖÐÄú²»µÃ²»É¾³ýÎÞÓõÄͼ±êºÍ²Ëµ¥¡£
DrillµÄÖ÷Òª²¿·Ö±»GtkPaned²¿¼þ»®·ÖΪÁ½¸ö¹¤×÷¿Õ¼ä¡££¨¼ûÏÂͼ£©
Á½¸ö¹¤×÷¿Õ¼ä±»´¹Ö±»®·Ö£¬ÖмäµÄ¿É¿Ø±ß½ç¿ÉÒÔ×óÓÒÒÆ¶¯£¬ÓÃÒÔµ÷Õûÿ¸ö¹¤×÷¿Õ¼äµÄ´óС¡£×ó±ßµÄ¹¤×÷¿Õ¼ä°üº¬ÓÐÊ÷×´½á¹¹²¿¼þ£¨GtkTree£©£¬ÓұߵŤ×÷¿Õ¼äÔÝʱÊǿյģ¬ÒÔºó¾ÍÊÇÔÚÕâ¸öλÖøù¾ÝʹÓÃÕßµÄÒªÇóÌí¼Ó·ÖÖ§¡£
ͨ¹ýGlade²é¿´DrillµÄ½çÃæÈÃÎÒÃÇÄܹ»½øÒ»²½Á˽âËüµÄ¸÷¸ö×é³ÉµÄ¹¹¼Ü¡££¨¼ûÏÂͼ£©
ÄúÔÚÉÏͼ Fig. 2 ÖпÉÒÔ¿´µ½£¬hpanedTree²¿¼þ (Á¥ÊôÓÚGtkPanedÀàÐÍ)½ö½ö°üº¬ÁËÒ»¸ö²¿¼þ, ¼´×ó±ßµÄframe2(Á¥ÊôÓÚGtkFrameÀàÐÍ)£¬ frame2°üº¬ÁËexerciceTree²¿¼þ¡£ËüÄܸüºÃµØ½«Ò»¸öGtkFrame²¿¼þµ¼ÈëÒ»¸öGtkPaned²¿¼þÖУ¨Ê¹ÓÃGTK_SHADOW_INÀàÐ͵Äshadow£©,ÕâÑù¾Í±ÜÃâÁËÕÚµ²×¡¿É¿Ø±ß½ç¡£
Ò»¸öGnome¶Ô»°¿ò¡°About Drill¡±¿´ÆðÀ´¾ÍÏóÏÂÃæÕâÑù£º
ËüµÄ²»Í¬²¿·Ö¿ÉÒÔͨ¹ýGladeÔÚ²¿¼þµÄÊôÐÔ´°¿ÚÖÐÐ޸ġ£
²¿¼þºÍ´¦Àíº¯ÊýµÄÃû³Æ£º
ÔÚPythonÏÂÓÃÏÂÊöµÄÕâЩ²¿¼þÃû³ÆÀ´Ê¹ÓÃËüÃÇ£º
Äú¿ÉÒÔÔÚͼ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²¿¼þ¡£
ÍêÕûµÄÔ´Âë¿ÉÒÔÔÚÕâÆªÎĵµµÄ×îºóÕÒµ½£¬ÄúÐèÒª½«ËüºÍdrill.glade±£´æÔÚͬһ¸öĿ¼Ï¡£
from gtk import *
from gnome.ui import *
from GDK import *
from libglade import *
ÀûÓÃ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+µÄ²Î¿¼ÊÖ²á £¨¹ØÓÚGtkLabelºÍGtkPaned²¿¼þ£©ÒÔ¼°PythonϵÄÔ´Âë /usr/lib/python1.5/site-packages/gtk.py ¿ÉÒÔ°ïÖúÄúÀí½â²¢ÕýȷʹÓò¿¼þ¡£
ÕâÊÇĿǰÕâÆªÎÄÕµÄÖØµã²¿·Ö£ºÈçºÎÓ¦ÓÃ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 ÊôÐÔµÄÖµÀ´¸üÐÂÎı¾±êÇ©£¨Óұߵģ©¡£
ÎÒÃǽö½ö½¨Á¢ÁËÒ»¸ö»ù±¾¿ò¼ÜÓÃÓÚÌí¼Ó·ÖÖ§£¨±ÈÈçÐí¶àпª·¢µÄ²¿¼þ£©¡£ÎÒÃÇÖ÷ÒªÌÖÂÛÁË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±¨¸æ´íÎó»òÌá³öÒâ¼û |
·ÒëÐÅÏ¢:
|
2000-01-05, generated by lfparser version 2.23