Gtk3 with Python, TextView rising multiple 'mark-set' signals

谁说我不能喝 提交于 2019-12-11 00:25:39

问题


Consider the following example code, which puts a TextView inside a window and registers the mark-set event:

#!/usr/bin/env python3

from gi.repository import Gtk

win = Gtk.Window(title='test')
text_view = Gtk.TextView()

def test (*args):
    print('test!')

win.add(text_view)
text_view.get_buffer().connect('mark-set', test)

win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

If I launch it, and I click on the visualized TextView once I get the debug output multiple times:

$ ./test.py 2>/dev/null
test!
test!
test!
test!

Do you know why this is happening? Is there a way of avoiding it?


回答1:


I don't know why exactly it's happening, but I found something to help you understand, or use it to prevent duplicate actions in handler, using mark.get_name(). Here I changed your code, type some text and play with it (select some text, click somewhere, ...)

#!/usr/bin/env python3
import time
from gi.repository import Gtk

win = Gtk.Window(title='test')
text_view = Gtk.TextView()

def mark_set(buf, itr, mark):
    ### mark.get_name() ==> 'selection_bound' | 'insert' | None
    print('Time: %.2f,    Mark Name: %s'%(time.time()%100, mark.get_name()))

win.add(text_view)
text_view.get_buffer().connect('mark-set', mark_set)

win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()


来源:https://stackoverflow.com/questions/13781496/gtk3-with-python-textview-rising-multiple-mark-set-signals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!