persistence gtk.view with shelve module -
i have difficult manage persistence of gtk.view object shelve module.
i use python 2.6.5 , pygtk.
my first test implemented process basic object this
#!/usr/bin/env python
-- coding: iso-8859-1 --
'''test de shelve ''' import gtk import gobject import shelve import os class shelvedemo(object): def init(self): self.variable1 ='essai' self.variable2 ='1' self.variable3 ='15' self.variable4 =15000
def affiche(self): print self.variable1 print self.variable2 print self.variable3 print self.variable4 def save(self,objet): save_file = os.path.abspath( "d:\sphinx" + os.sep + 'essai2.tvw') db = shelve.open(save_file) db['cle1']=objet print print "liste des clé enregistrée",db.keys print db.close() print('sauvegarde ok') def load(self): save_file = os.path.abspath("d:\sphinx" + os.sep + 'essai2.tvw') xx = shelve.open(save_file) print print "liste des clé loading....",xx.keys print objet1 = xx['cle1'] objet1.affiche() xx.close()
test = shelvedemo() test.affiche() test.save(test) test.load()
it's run ok . fine !!!!!!!
test 2 textview
#!/usr/bin/env python
-- coding: iso-8859-1 --
'''text widget/hypertext usually, tags modify appearance of text in view, e.g. making bold or colored or underlined. tags not restricted appearance. can affect behavior of mouse , key presses, demo shows.'''
pygtk version: maik hertha
import gtk import pango import gobject import shelve_whc shelve import os printv import printv class test_affiche: def init(self,bufferr): self.window = gtk.window() self.window.connect('destroy', lambda *w: gtk.main_quit()) self.window.set_title(self.class._name_+' clone') self.window.set_default_size(450, 450) self.window.set_border_width(0)
view = gtk.textview() view.set_buffer(bufferr) view.set_wrap_mode(gtk.wrap_word) #c'est utile car il y en permanence une partie cachée pas top pour une aide sw = gtk.scrolledwindow() sw.set_policy(gtk.policy_automatic, gtk.policy_automatic) self.window.add(sw) sw.add(view) self.window.show_all()
class hypertextdemo(gtk.window): hovering_over_link = false hand_cursor = gtk.gdk.cursor(gtk.gdk.hand2) regular_cursor = gtk.gdk.cursor(gtk.gdk.xterm) def save(self,bufferr): save_file = os.path.abspath("d:\sphinx" + os.sep + 'essai.tvw') db = shelve.open(save_file,protocol=2) # protocol=2 est nécessaire pour traiter objet complexe type textbuffer printv (buffer) print type(buffer) db['cle']=bufferr db.close() print('sauvegarde ok') # fonction de test pour savoir ce que je sauvegarde essai = test_affiche(bufferr) printv( bufferr)
def load(self): save_file = os.path.abspath("d:\sphinx" + os.sep + 'essai.tvw') db = shelve.open(save_file,protocol=2) # protocol=2 est nécessaire pour traiter objet complexe type textbuffer print print "liste des clé enregistrée",db.keys() print conteneur = db['cle'] print "type de l'objet en cours de chargement %s" %type(conteneur) print "nom de l'objet en cours de chargement %s" %conteneur.__class__.__name__ printv (conteneur) # fonction de test pour savoir ce que je sauvegarde essai3 = test_affiche(conteneur) db.close() def __init__(self, parent=none): gtk.window.__init__(self) try: self.set_screen(parent.get_screen()) except attributeerror: self.connect('destroy', lambda *w: gtk.main_quit()) self.set_title(self.__class__.__name__) self.set_default_size(450, 450) self.set_border_width(0) view = gtk.textview() view.set_wrap_mode(gtk.wrap_word) #c'est utile car il y en permanence une partie cachée pas top pour une aide view.connect("key-press-event", self.key_press_event) view.connect("event-after", self.event_after) view.connect("motion-notify-event", self.motion_notify_event) view.connect("visibility-notify-event", self.visibility_notify_event) buffer = view.get_buffer() sw = gtk.scrolledwindow() sw.set_policy(gtk.policy_automatic, gtk.policy_automatic) self.add(sw) sw.add(view) self.show_page(buffer, 1) self.show_all() self.save(view.get_buffer()) self.load() # links can activated pressing enter. def key_press_event(self, text_view, event): if (event.keyval == gtk.gdk.return or event.keyval == gtk.gdk.kp_enter): buffer = text_view.get_buffer() iter = buffer.get_iter_at_mark(buffer.get_insert()) self.follow_if_link(text_view, iter) return false # links can activated clicking. def event_after(self, text_view, event): if event.type != gtk.gdk.button_release: return false if event.button != 1: return false buffer = text_view.get_buffer() # shouldn't follow link if user has selected try: start, end = buffer.get_selection_bounds() except valueerror: # if there nothing selected, none return pass else: if start.get_offset() != end.get_offset(): return false x, y = text_view.window_to_buffer_coords(gtk.text_window_widget, int(event.x), int(event.y)) iter = text_view.get_iter_at_location(x, y) self.follow_if_link(text_view, iter) return false # looks @ tags covering position (x, y) in text view, # , if 1 of them link, change cursor "hands" cursor # typically used web browsers. def set_cursor_if_appropriate(self, text_view, x, y): hovering = false buffer = text_view.get_buffer() iter = text_view.get_iter_at_location(x, y) tags = iter.get_tags() tag in tags: page = tag.get_data("page") if page != 0: hovering = true break if hovering != self.hovering_over_link: self.hovering_over_link = hovering if self.hovering_over_link: text_view.get_window(gtk.text_window_text).set_cursor(self.hand_cursor) else: text_view.get_window(gtk.text_window_text).set_cursor(self.regular_cursor) # update cursor image if pointer moved. def motion_notify_event(self, text_view, event): x, y = text_view.window_to_buffer_coords(gtk.text_window_widget, int(event.x), int(event.y)) self.set_cursor_if_appropriate(text_view, x, y) text_view.window.get_pointer() return false # update cursor image if window becomes visible # (e.g. when window covering got iconified). def visibility_notify_event(self, text_view, event): wx, wy, mod = text_view.window.get_pointer() bx, = text_view.window_to_buffer_coords(gtk.text_window_widget, wx, wy) self.set_cursor_if_appropriate (text_view, bx, by) return false def insert_link(self, buffer, iter, text, page): ''' inserts piece of text buffer, giving usual appearance of hyperlink in web browser: blue , underlined. additionally, attaches data on tag, make recognizable link. ''' tag = buffer.create_tag(none, foreground="blue", underline=pango.underline_single) tag.set_data("page", page) buffer.insert_with_tags(iter, text, tag) def show_page(self, buffer, page): ''' fills buffer text , interspersed links. in real hypertext app, method parse file identify links. ''' buffer.set_text("", 0) iter = buffer.get_iter_at_offset(0) if page == 1: buffer.insert(iter, "some text show simple ") self.insert_link(buffer, iter, "hypertext", 3) buffer.insert(iter, " can realized ") self.insert_link(buffer, iter, "tags", 2) buffer.insert(iter, ".") buffer.insert(iter, "\n") buffer.insert(iter, 'inserts piece of text buffer, giving usual' 'appearance of hyperlink in web browser: blue , underlined.' 'additionally, attaches data on tag, make recognizable' 'as link') buffer.insert(iter, "\n") buffer.insert(iter, 'inserts piece of text buffer, giving usual' 'appearance of hyperlink in web browser: blue , underlined.' 'additionally, attaches data on tag, make recognizable' 'as link') elif page == 2: buffer.insert(iter, "a tag attribute can applied range of text. " "for example, tag might called \"bold\" , make text inside " "the tag bold. however, tag concept more general " "tags don't have affect appearance. can instead affect " "behavior of mouse , key presses, \"lock\" range of text " "user can't edit it, or countless other things.\n", -1) self.insert_link(buffer, iter, "go back", 1) elif page == 3: tag = buffer.create_tag(none, weight=pango.weight_bold) buffer.insert_with_tags(iter, "hypertext:\n", tag) buffer.insert(iter, "machine-readable text not sequential organized " "so related items of information connected.\n") self.insert_link(buffer, iter, "go back", 1) def follow_if_link(self, text_view, iter): ''' looks @ tags covering position of iter in text view, , if 1 of them link, follow showing page identified data attached it. ''' tags = iter.get_tags() tag in tags: page = tag.get_data("page") if page != 0: self.show_page(text_view.get_buffer(), page) break
def main():
hypertextdemo() gtk.main()
if name == 'main':
main()
concerning test 2, reload action not ok if persistence action not occur
can have explanation mistake thank in advance help
hi sorry it's small mistake other people
replace import shelve_whc shelve
by import shelve
Comments
Post a Comment