问题
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