gpt4 book ai didi

python - 在 pyGTK 的 TextView/TextBuffer 中插入粗体字符

转载 作者:太空狗 更新时间:2023-10-30 02:50:12 24 4
gpt4 key购买 nike

我有一个 TextView 和一个与其关联的 TextBuffer。

当用户按下 Ctrl+b 时,我希望文本开始以粗体输入,直到用户再次按下 Ctrl+b。

我尝试了自己的方法,但没有用,然后我在邮件列表中找到了这篇文章: http://www.daa.com.au/pipermail/pygtk/2009-April/016951.html

和我一样的问题,有人给出的解决方案是

Your application will have to handle the bookkeeping required to managethe tags in the TextBuffer. When text is inserted at the cursor your apphas to catch a signal indicating the text is being inserted and thenapply the required tags to the inserted text. I think this can be doneby catching theTextBuffer "insert-text" signal (using connect_after()to make sure the text has already been inserted) and then applying thetags to the text in the callback.

所以我尝试了这个。这是我的 textbuffer.py

import gtk
import pango

class TextBuffer(gtk.TextBuffer):
def __init__(self):
gtk.TextBuffer.__init__(self)
self.connect_after('insert-text', self.text_inserted)
# A list to hold our active tags
self.tags_on = []
# Our Bold tag.
self.tag_bold = self.create_tag("bold", weight=pango.WEIGHT_BOLD)

def get_iter_position(self):
return self.get_iter_at_mark(self.get_insert())

def make_bold(self, text):
''' add "bold" to our active tags list '''
self.tags_on.append('bold')

def text_inserted(self, buffer, iter, text, length):
# A text was inserted in the buffer. If there are ny tags in self.tags_on, apply them
if self.tags_on:
print self.get_iter_position()

# This sets the iter back N characters
iter.backward_chars(length)

# And this applies tag from iter to end of buffer
self.apply_tag_by_name('bold', self.get_iter_position(), self.get_end_iter())

print self.get_iter_position()

每当有人按下 Ctrl+b 时,主脚本就会调用 make_bold() 方法。

从理论上讲,这正是邮件帮助所说的那样。但是没有用。文本在我键入时未显示为粗体。如果我按向左箭头并将光标向后移动,然后键入一个字符,则光标右侧的字符会变为粗体。

我怎样才能完成这个任务?

此外,有人可以向其中添加标签“textbuffer”吗?我无法创建新标签,我觉得该标签比“textview”更准确

最佳答案

在示例代码中,您在 TextBuffer.text_inserted 中调用了 iter.backward_chars但你永远不会使用那个迭代器!,所以我制作了一个示例脚本来向你展示所需的行为并澄清:

import gtk
import pango

class TextBuffer(gtk.TextBuffer):
def __init__(self):
gtk.TextBuffer.__init__(self)
self.connect_after('insert-text', self.text_inserted)
# A list to hold our active tags
self.tags_on = []
# Our Bold tag.
self.tag_bold = self.create_tag("bold", weight=pango.WEIGHT_BOLD)

def get_iter_position(self):
return self.get_iter_at_mark(self.get_insert())

def make_bold(self):
''' add "bold" to our active tags list '''
if 'bold' in self.tags_on:
del self.tags_on[self.tags_on.index('bold')]
else:
self.tags_on.append('bold')

def text_inserted(self, buffer, iter, text, length):
# A text was inserted in the buffer. If there are ny tags in self.tags_on, apply them
if self.tags_on:
# This sets the iter back N characters
iter.backward_chars(length)
# And this applies tag from iter to end of buffer
self.apply_tag_by_name('bold', self.get_iter_position(), iter)



def main():
window = gtk.Window()
window.connect('destroy', lambda _: gtk.main_quit())
window.resize(300, 300)
tb = TextBuffer()
tv = gtk.TextView(buffer=tb)

accel = gtk.AccelGroup()
accel.connect_group(gtk.keysyms.b,
gtk.gdk.CONTROL_MASK,gtk.ACCEL_LOCKED,
lambda a,b,c,d: tb.make_bold())
window.add_accel_group(accel)
window.add(tv)
window.show_all()
gtk.main()

if __name__ == '__main__':
main()

关于python - 在 pyGTK 的 TextView/TextBuffer 中插入粗体字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4251658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com