当我将文件拖放到条目时,gtk.entry.connect('drag-data-received', callback)
回调方法运行两次。但是它应该只运行一次。为什么叫两次?
#!/usr/bin/env python3
#
# 2019-05-04 19:29:22
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as g, Gdk as d
class Window(g.Window):
def __init__(self):
super().__init__(title = 'hi')
self.grid = g.Grid()
self.add(self.grid)
self.count = 1
entry = g.Entry()
entry.drag_dest_set(g.DestDefaults.ALL,
[g.TargetEntry.new("text/uri-list", 0, 80)],
d.DragAction.COPY)
entry.connect('drag-data-received', self.set_path_by_drag)
self.grid.add(entry)
def set_path_by_drag(self, entry, context, x, y, data, info, time):
'''
reference: https://stackoverflow.com/questions/24094186/drag-and-drop-file-example-in-pygobject
'''
print(self.count)
self.count += 1
return True
def main():
win = Window()
win.connect('destroy', g.main_quit)
win.show_all()
g.main()
if __name__ == '__main__':
main()
输出:
1
2
我已经安装了以下libgtk-3软件包:
$ dpkg -l | grep libgtk-3
ii libgtk-3-0:amd64 3.22.30-1ubuntu3 amd64 GTK+ graphical user interface library
ii libgtk-3-bin 3.22.30-1ubuntu3 amd64 programs for the GTK+ graphical user interface library
ii libgtk-3-common 3.22.30-1ubuntu3 all common files for the GTK+ graphical user interface library