2

考虑以下示例代码,它将 aTextView放入窗口并注册mark-set事件:

#!/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()

如果我启动它,并在TextView 多次获得调试输出后单击可视化:

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

你知道为什么会这样吗?有没有办法避免呢?

4

1 回答 1

0

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()
于 2015-07-11T11:50:44.750 回答