带有搁置模块的 persistence gtk.view
我很难使用搁置模块来管理 gtk.view 对象的持久性。
我使用 python 2.6.5 和 PyGtk。
我的第一次测试 我使用这样的基本对象实现了流程
#!/usr/bin/env python
-- 编码:iso-8859-1 --
'''test de shelve ''' 导入gtk 导入gobject 进口货架 导入操作系统 类 ShelveDemo(对象): def init(自身): 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()
测试 = ShelveDemo() 测试.affich() 测试.保存(测试) test.load()
运行正常。美好的 !!!!!!!
使用 textView 进行测试 2
#!/usr/bin/env python
-- 编码:iso-8859-1 --
'''文本小部件/超文本 通常,标签会修改视图中文本的外观,例如使其 粗体、彩色或下划线。但标签并不局限于外观。 它们还可以影响鼠标和按键的行为,如本演示所示 显示。'''
pygtk 版本:Maik Hertha
import gtk 导入潘戈 导入gobject 导入 shelve_whc 作为搁置 导入操作系统 从 printv 导入 printv 类 test_affiche: def init(self,bufferr): self.window = gtk.Window() self.window.connect('销毁', lambda *w: gtk.main_quit()) self.window.set_title(self.类._名称_+'克隆') 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 a 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()
类 HypertextDemo(gtk.Window): 悬停在链接上=假 hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2) 常规光标 = gtk.gdk.Cursor(gtk.gdk.XTERM) def 保存(自身,缓冲区): save_file = os.path.abspath("D:\sphinx" + os.sep + 'essai.tvw') db = shelve.open(save_file,protocol=2) # protocol=2 是复合对象类型 TextBuffer 所必需的 printv(缓冲区) 打印类型(缓冲) db['cle']=缓冲区 db.close() print('sauvegarde 好的') # 测试你的能力的功能 essai = test_affiche(缓冲区) 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 a 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 be activated by 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 also be activated by 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()
# we shouldn't follow a link if the user has selected something
try:
start, end = buffer.get_selection_bounds()
except ValueError:
# If there is nothing selected, None is 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 at all tags covering the position (x, y) in the text view,
# and if one of them is a link, change the cursor to the "hands" cursor
# typically used by 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()
for 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 the cursor image if the 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
# Also update the cursor image if the window becomes visible
# (e.g. when a window covering it got iconified).
def visibility_notify_event(self, text_view, event):
wx, wy, mod = text_view.window.get_pointer()
bx, by = 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 a piece of text into the buffer, giving it the usual
appearance of a hyperlink in a web browser: blue and underlined.
Additionally, attaches some data on the tag, to make it recognizable
as a 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 the buffer with text and interspersed links. In any real
hypertext app, this method would parse a file to identify the links.
'''
buffer.set_text("", 0)
iter = buffer.get_iter_at_offset(0)
if page == 1:
buffer.insert(iter, "Some text to show that simple ")
self.insert_link(buffer, iter, "hypertext", 3)
buffer.insert(iter, " can easily be realized with ")
self.insert_link(buffer, iter, "tags", 2)
buffer.insert(iter, ".")
buffer.insert(iter, "\n")
buffer.insert(iter, 'Inserts a piece of text into the buffer, giving it the usual'
'appearance of a hyperlink in a web browser: blue and underlined.'
'Additionally, attaches some data on the tag, to make it recognizable'
'as a link')
buffer.insert(iter, "\n")
buffer.insert(iter, 'Inserts a piece of text into the buffer, giving it the usual'
'appearance of a hyperlink in a web browser: blue and underlined.'
'Additionally, attaches some data on the tag, to make it recognizable'
'as a link')
elif page == 2:
buffer.insert(iter,
"A tag is an attribute that can be applied to some range of text. "
"For example, a tag might be called \"bold\" and make the text inside "
"the tag bold. However, the tag concept is more general than that "
"tags don't have to affect appearance. They can instead affect the "
"behavior of mouse and key presses, \"lock\" a range of text so the "
"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 that is not sequential but is organized "
"so that related items of information are connected.\n")
self.insert_link(buffer, iter, "Go back", 1)
def follow_if_link(self, text_view, iter):
''' Looks at all tags covering the position of iter in the text view,
and if one of them is a link, follow it by showing the page identified
by the data attached to it.
'''
tags = iter.get_tags()
for 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()
关于测试 2,重新加载操作是不行的,就像持久化操作没有发生一样
我可以吗一些解释是我的错误 预先感谢您的帮助
I have some difficult to manage persistence of gtk.view object with shelve module.
I use python 2.6.5 and PyGtk.
MY FIRST TEST
I implemented process with basic object like 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 with textView
#!/usr/bin/env python
-- coding: iso-8859-1 --
'''Text Widget/Hypertext
Usually, tags modify the appearance of text in the view, e.g. making it
bold or colored or underlined. But tags are not restricted to appearance.
They can also affect the behavior of mouse and key presses, as this demo
shows.'''
pygtk version: Maik Hertha
import gtk
import pango
import gobject
import shelve_whc as shelve
import os
from 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 a 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 a 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 be activated by 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 also be activated by 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()
# we shouldn't follow a link if the user has selected something
try:
start, end = buffer.get_selection_bounds()
except ValueError:
# If there is nothing selected, None is 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 at all tags covering the position (x, y) in the text view,
# and if one of them is a link, change the cursor to the "hands" cursor
# typically used by 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()
for 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 the cursor image if the 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
# Also update the cursor image if the window becomes visible
# (e.g. when a window covering it got iconified).
def visibility_notify_event(self, text_view, event):
wx, wy, mod = text_view.window.get_pointer()
bx, by = 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 a piece of text into the buffer, giving it the usual
appearance of a hyperlink in a web browser: blue and underlined.
Additionally, attaches some data on the tag, to make it recognizable
as a 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 the buffer with text and interspersed links. In any real
hypertext app, this method would parse a file to identify the links.
'''
buffer.set_text("", 0)
iter = buffer.get_iter_at_offset(0)
if page == 1:
buffer.insert(iter, "Some text to show that simple ")
self.insert_link(buffer, iter, "hypertext", 3)
buffer.insert(iter, " can easily be realized with ")
self.insert_link(buffer, iter, "tags", 2)
buffer.insert(iter, ".")
buffer.insert(iter, "\n")
buffer.insert(iter, 'Inserts a piece of text into the buffer, giving it the usual'
'appearance of a hyperlink in a web browser: blue and underlined.'
'Additionally, attaches some data on the tag, to make it recognizable'
'as a link')
buffer.insert(iter, "\n")
buffer.insert(iter, 'Inserts a piece of text into the buffer, giving it the usual'
'appearance of a hyperlink in a web browser: blue and underlined.'
'Additionally, attaches some data on the tag, to make it recognizable'
'as a link')
elif page == 2:
buffer.insert(iter,
"A tag is an attribute that can be applied to some range of text. "
"For example, a tag might be called \"bold\" and make the text inside "
"the tag bold. However, the tag concept is more general than that "
"tags don't have to affect appearance. They can instead affect the "
"behavior of mouse and key presses, \"lock\" a range of text so the "
"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 that is not sequential but is organized "
"so that related items of information are connected.\n")
self.insert_link(buffer, iter, "Go back", 1)
def follow_if_link(self, text_view, iter):
''' Looks at all tags covering the position of iter in the text view,
and if one of them is a link, follow it by showing the page identified
by the data attached to it.
'''
tags = iter.get_tags()
for 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 is not ok like if persistence action do not occur
Can I have some explanation were is my mistake
Thank you in advance for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,抱歉,这是其他人
替换 import shelve_whc as shelve的一个小错误
用 import shelve
Hi sorry It's a small mistake for other people
replace import shelve_whc as shelve
by import shelve