关键是创建另一个迭代器(我使用字典)来跟踪序列化文本,然后当我连续单击时,如果值为字节,它会尝试使用反序列化函数,因为它不会简单地使用 set_text 设置文本。
在反序列化之前将文本设置为空也很重要 set_text('') ,否则缓冲区的先前值将放置在缓冲区的当前值之前。
至于文本的更改,我使用方法 connect 连接“更改”信号并将更改序列化并将序列化值传递给字典。这就是我得到的:
# dictionary to track the tags
self.dict_any_change_front = {str(key): value[1] for key, value in enumerate(self.sub_list_store)}
self.dict_any_change_back = {str(key): value[1] for key, value in enumerate(self.sub_list_store_back)}
def deserialize(self, text_buffer, exported):
text_buffer.set_text('')
text_buffer.deserialize( text_buffer,
text_buffer.register_deserialize_tagset(),
text_buffer.get_start_iter(),
exported )
def item_selected(self, *args):
# Need this try/except to silent a indexerror that will occur case the second window close and if opened again,
# merely cosmetic as it will always occur, just select any row and all good.
# The get_selected_rows()[1] will return a empty list at first try when reopening the second window, I just don't know why
try:
path = self.selected_row.get_selected_rows()[1][0]
exported_front = self.dict_any_change_front[str(path)]
exported_back = self.dict_any_change_back[str(path)]
try:
if isinstance(exported_front, bytes):
self.deserialize(self.text_buffer_front, exported_front)
else:
self.text_buffer_front.set_text(self.sub_list_store[path][1])
if isinstance(exported_back, bytes):
self.deserialize(self.text_buffer_back, exported_back)
else:
self.text_buffer_back.set_text(self.sub_list_store_back[path][1])
except:
self.text_buffer_front.set_text(self.sub_list_store[path][1])
self.text_buffer_back.set_text(self.sub_list_store_back[path][1])
self.text_buffer_front.connect('changed', self.editingCard)
self.text_buffer_back.connect('changed', self.editingCardBack)
except IndexError:
pass
def editingCard(self, text_buffer):
path = self.selected_row.get_selected_rows()[1][0]
start_iter_front = text_buffer.get_start_iter()
end_iter_front = text_buffer.get_end_iter()
self.sub_list_store[path][1] = text_buffer.get_text(start_iter_front, end_iter_front, True)
format = text_buffer.register_serialize_tagset()
exported = text_buffer.serialize( text_buffer,
format,
start_iter_front,
end_iter_front )
self.dict_any_change_front[str(path)] = exported
def editingCardBack(self, text_buffer):
path = self.selected_row.get_selected_rows()[1][0]
start_iter_back = text_buffer.get_start_iter()
end_iter_back = text_buffer.get_end_iter()
self.sub_list_store_back[path][1] = text_buffer.get_text(start_iter_back, end_iter_back, True)
format = text_buffer.register_serialize_tagset()
exported = text_buffer.serialize( text_buffer,
format,
start_iter_back,
end_iter_back )
self.dict_any_change_back[str(path)] = exported
def on_toolbar_button_clicked(self, widget, tag_front, tag_back):
bounds_front = self.text_buffer_front.get_selection_bounds()
bounds_back = self.text_buffer_back.get_selection_bounds()
path = self.selected_row.get_selected_rows()[1][0]
##### FRONT
if len(bounds_front) != 0:
(start, end) = bounds_front
selection_front = self.text_buffer_front.get_text(start, end, True)
get_insert_front = self.text_buffer_front.get_insert()
self.text_buffer_front.apply_tag(tag_front, start, end)
start_iter_front = self.text_buffer_front.get_start_iter()
end_iter_front = self.text_buffer_front.get_end_iter()
format = self.text_buffer_front.register_serialize_tagset()
exported = self.text_buffer_front.serialize( self.text_buffer_front,
format,
start_iter_front,
end_iter_front )
self.dict_any_change_front[str(path)] = exported
###### BACK
if len(bounds_back) != 0:
(start, end) = bounds_back
selection_back = self.text_buffer_back.get_text(start, end, True)
get_insert_back = self.text_buffer_back.get_insert()
self.text_buffer_back.apply_tag(tag_back, start, end)
start_iter_back = self.text_buffer_back.get_start_iter()
end_iter_back = self.text_buffer_back.get_end_iter()
format = self.text_buffer_back.register_serialize_tagset()
exported = self.text_buffer_back.serialize( self.text_buffer_back,
format,
start_iter_back,
end_iter_back )
self.dict_any_change_back[str(path)] = exported
按我的意愿工作:)。
编辑
我调整了我的代码以在开始时序列化所有内容并将其放入字典中,而不是将字符串放入字典中,并且编辑文本序列化文本并将其放入字典中,这样可以删除一些 if/else 和 try/except 的.
我还创建了序列化和反序列化的函数,并将这些函数放在另一个文件中,我认为这种方式更好。
...
from myfuncfile import serializeIt, deserializeIt
...
# dictionary to track the tags
self.dict_any_change_front = {str(key): serializeIt(text_buffer=self.text_buffer_front, tmp_string=value[1]) \
for key, value in enumerate(self.sub_list_store)}
self.dict_any_change_back = {str(key): serializeIt(text_buffer=self.text_buffer_back, tmp_string=value[1]) \
for key, value in enumerate(self.sub_list_store_back)}
def item_selected(self, *args):
# Silencing a indexerror that will occur in case the window was hided and rised again
# it is not important, can be ignored
try:
path = self.selected_row.get_selected_rows()[1][0]
exported_front = self.dict_any_change_front[str(path)]
exported_back = self.dict_any_change_back[str(path)]
deserializeIt(self.text_buffer_front, exported_front)
deserializeIt(self.text_buffer_back, exported_back)
self.text_buffer_front.connect('changed', self.editingCard)
self.text_buffer_back.connect('changed', self.editingCardBack)
except IndexError:
pass
def editingCard(self, text_buffer_front):
# Silencing a indexerror that will occur in case the window was hided and rised again
# it is not important, can be ignored
try:
path = self.selected_row.get_selected_rows()[1][0]
start_iter_front = text_buffer_front.get_start_iter()
end_iter_front = text_buffer_front.get_end_iter()
self.sub_list_store[path][1] = text_buffer_front.get_text(start_iter_front, end_iter_front, True)
exported = serializeIt(text_buffer=text_buffer_front)
self.dict_any_change_front[str(path)] = exported
except IndexError:
pass
def editingCardBack(self, text_buffer_back):
# Silencing a indexerror that will occur in case the window was hided and rised again
# it is not important, can be ignored
try:
path = self.selected_row.get_selected_rows()[1][0]
start_iter_back = text_buffer_back.get_start_iter()
end_iter_back = text_buffer_back.get_end_iter()
self.sub_list_store_back[path][1] = text_buffer_back.get_text(start_iter_back, end_iter_back, True)
exported = serializeIt(text_buffer=text_buffer_back)
self.dict_any_change_back[str(path)] = exported
except IndexError:
pass
def on_toolbar_button_clicked(self, widget, tag_front, tag_back):
bounds_front = self.text_buffer_front.get_selection_bounds()
bounds_back = self.text_buffer_back.get_selection_bounds()
path = self.selected_row.get_selected_rows()[1][0]
##### FRONT
if len(bounds_front) != 0:
(start, end) = bounds_front
selection_front = self.text_buffer_front.get_text(start, end, True)
get_insert_front = self.text_buffer_front.get_insert()
self.text_buffer_front.apply_tag(tag_front, start, end)
exported = serializeIt(text_buffer=self.text_buffer_front)
self.dict_any_change_front[str(path)] = exported
###### BACK
if len(bounds_back) != 0:
(start, end) = bounds_back
selection_back = self.text_buffer_back.get_text(start, end, True)
get_insert_back = self.text_buffer_back.get_insert()
self.text_buffer_back.apply_tag(tag_back, start, end)
exported = serializeIt(text_buffer=self.text_buffer_back)
self.dict_any_change_back[str(path)] = exported
...
...
def serializeIt(text_buffer, tmp_string=None):
if tmp_string:
text_buffer.set_text(tmp_string)
tmp_start_iter = text_buffer.get_start_iter()
tmp_end_iter = text_buffer.get_end_iter()
tmp_format = text_buffer.register_serialize_tagset()
tmp_exported = text_buffer.serialize( text_buffer,
tmp_format,
tmp_start_iter,
tmp_end_iter )
return tmp_exported
else:
start_iter = text_buffer.get_start_iter()
end_iter = text_buffer.get_end_iter()
format = text_buffer.register_serialize_tagset()
exported = text_buffer.serialize( text_buffer,
format,
start_iter,
end_iter )
return exported
def deserializeIt(text_buffer, exported):
text_buffer.set_text('')
text_buffer.deserialize(text_buffer,
text_buffer.register_deserialize_tagset(),
text_buffer.get_start_iter(),
exported )
...