Bin*_*V A 4 python gtk drag-and-drop gdk
我正在创建一个小应用程序必须能够接收URL.如果应用程序窗口打开,我应该能够从浏览器拖动链接并将其放入应用程序 - 应用程序将URL保存到数据库.
我在Python/GTk中创建它.但我对它的拖放功能有点困惑.那么,怎么做?
一些示例代码实现拖放(我的应用程序使用了一些此代码)...
import pygtk
pygtk.require('2.0')
import gtk
# function to print out the mime type of the drop item
def drop_cb(wid, context, x, y, time):
l.set_text('\n'.join([str(t) for t in context.targets]))
# What should I put here to get the URL of the link?
context.finish(True, False, time)
return True
# Create a GTK window and Label, and hook up
# drag n drop signal handlers to the window
w = gtk.Window()
w.set_size_request(200, 150)
w.drag_dest_set(0, [], 0)
w.connect('drag_drop', drop_cb)
w.connect('destroy', lambda w: gtk.main_quit())
l = gtk.Label()
w.add(l)
w.show_all()
# Start the program
gtk.main()
Run Code Online (Sandbox Code Playgroud)
您必须自己获取数据.这是一个简单的工作示例,它将为丢弃的URL设置标签:
#!/usr/local/env python
import pygtk
pygtk.require('2.0')
import gtk
def motion_cb(wid, context, x, y, time):
l.set_text('\n'.join([str(t) for t in context.targets]))
context.drag_status(gtk.gdk.ACTION_COPY, time)
# Returning True which means "I accept this data".
return True
def drop_cb(wid, context, x, y, time):
# Some data was dropped, get the data
wid.drag_get_data(context, context.targets[-1], time)
return True
def got_data_cb(wid, context, x, y, data, info, time):
# Got data.
l.set_text(data.get_text())
context.finish(True, False, time)
w = gtk.Window()
w.set_size_request(200, 150)
w.drag_dest_set(0, [], 0)
w.connect('drag_motion', motion_cb)
w.connect('drag_drop', drop_cb)
w.connect('drag_data_received', got_data_cb)
w.connect('destroy', lambda w: gtk.main_quit())
l = gtk.Label()
w.add(l)
w.show_all()
gtk.main()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4536 次 |
| 最近记录: |