示例:
将文件放入 glade 设计的窗口中,它会触发 Handler.open_from_path(path)
答案1
将窗口“main_win”Widget drag_data_received 回调设置为 on_main_win_drag_data_received。URI 打印在状态栏中。
from gi.repository import Gtk, Gdk
builder = Gtk.Builder()
builder.add_from_file("assets/ui/design.glade")
class UI():
def __init__(self):
#get window
self.window = builder.get_object("main_win")
#add drop target
self.window.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
self.window.drag_dest_add_uri_targets()
self.window.set_title("Main Window")
#show the window
self.window.show_all()
#get an instance of the gui specific code
ui = UI()
class Worker():
"""
Real worker code
"""
#get a worker instance
worker = Worker()
def log(uri):
status = builder.get_object("status_bar")
context = status.get_context_id('')
status.pop(context)
status.push(context, uri.rstrip())
class Handler():
"""
Gtk callback handlers
"""
def on_main_win_delete_event(self, *args, **kwds):
Gtk.main_quit()
def on_main_win_drag_data_received(self, widget, drag_context, x, y, data, info, time):
log(data.get_data())
#connect handlers
builder.connect_signals(Handler())
if __name__ == '__main__':
Gtk.main()
已经测试并可用。