Python GTK Drag and Drop - Get URL(Python GTK 拖放 - 获取 URL)
问题描述
我正在创建一个小应用程序必须能够接收 URL.如果应用程序窗口打开,我应该能够从浏览器中拖动一个链接并将其拖放到应用程序中 - 应用程序会将 URL 保存到数据库中.
I'm creating a small app must be able to receive URLs. If the apps window is open, I should be able to drag a link from a browser and drop it into the app - and the app will save the URL to a database.
我正在 Python/GTk 中创建它.但我对其中的拖放功能有点困惑.那么,该怎么做呢?
I'm creating this in Python/GTk. But I am a bit confused about the drag and drop functionality in it. So, how do it?
一些实现拖放的示例代码(我的应用程序使用了一些此代码)...
Some sample code to implement drag/drop(my app uses a bit of this code)...
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('
'.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()
推荐答案
您必须自己获取数据.这是一个简单的工作示例,它将为删除的 url 设置标签:
You must fetch the data yourself. Here's a simple working example that will set a label to the url dropped:
#!/usr/local/env python
import pygtk
pygtk.require('2.0')
import gtk
def motion_cb(wid, context, x, y, time):
l.set_text('
'.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()
这篇关于Python GTK 拖放 - 获取 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python GTK 拖放 - 获取 URL
基础教程推荐
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01