大约两个星期以来,我一直在学习 GTK +3.0。我想做一个好的桌面应用,但是越来越觉得GTK,比较神秘,我一直是为web开发的,6个多月的android开发,我有一年Windows Presentation Foundation的经验,从来没有发现这么难做任何事情,比如 GTK,它现在正试图在应用程序中实现拖动文件,最后我找到了一个示例,虽然它的工作原理并不完全理解对某些代码行的需求。
所需的解释是关于方法的Gtk.Window.drag_dest_set
,Window.on_drag_motion
并且Window.on_drag_drop
为什么需要使用Gdk.drag_status
?为什么我必须这样做widget.drag_get_data(context, context.list_targets()[-1], time)
?
from gi.repository import Gtk, Gdk
class Window(Gtk.Window):
'''
Main Window
'''
def __init__(self):
super(Window, self).__init__(title=TITLE)
self.connect('delete-event', Gtk.main_quit)
'''
set up drag
'''
self.connect('drag-motion', self.on_drag_motion)
self.connect('drag-drop', self.on_drag_drop)
self.connect('drag-data-received', self.on_drag_data_received)
self.drag_dest_set(0, [], 0)
self.show()
Gtk.main()
def on_drag_motion(self, widgt, context, c, y, time):
Gdk.drag_status(context, Gdk.DragAction.COPY, time)
return True
def on_drag_drop(self, widget, context, x, y, time):
widget.drag_get_data(context, context.list_targets()[-1], time)
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
files = data.get_text().rstrip('\n').split('\n')
for file in files:
print(file)
if __name__ == '__main__':
Window()