Tk没有任何命令来处理这个问题,Python也没有任何额外的Tk扩展来执行拖放应用程序,因此您需要一个扩展来执行这个操作。Tkdnd(位于http://sourceforge.net/projects/tkdnd的Tk扩展,而不是Tkdnd.py模块)对我有效。要从Python中使用它,需要一个包装器。快速搜索一个,似乎http://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html包含这样的代码。我又做了一个,因为我不喜欢另一个。我的包装器的问题是它高度未经测试,实际上我只使用了bindtarget函数,而且只使用了10秒左右。

使用下面的包装器,您可以创建一些小部件并声明它支持接收拖动的文件。下面是一个例子:# The next two lines are not necessary if you installed TkDnd

# in a proper place.

import os

os.environ['TKDND_LIBRARY'] = DIRECTORYTOTHETKDNDBINARY

import Tkinter

from untested_tkdnd_wrapper import TkDND

root = Tkinter.Tk()

dnd = TkDND(root)

entry = Tkinter.Entry()

entry.pack()

def handle(event):

event.widget.insert(0, event.data)

dnd.bindtarget(entry, handle, 'text/uri-list')

root.mainloop()

这里是untested_tkdnd_wrapper.py的代码:import os

import Tkinter

def _load_tkdnd(master):

tkdndlib = os.environ.get('TKDND_LIBRARY')

if tkdndlib:

master.tk.eval('global auto_path; lappend auto_path {%s}' % tkdndlib)

master.tk.eval('package require tkdnd')

master._tkdnd_loaded = True

class TkDND(object):

def __init__(self, master):

if not getattr(master, '_tkdnd_loaded', False):

_load_tkdnd(master)

self.master = master

self.tk = master.tk

# Available pre-defined values for the 'dndtype' parameter:

# text/plain

# text/plain;charset=UTF-8

# text/uri-list

def bindtarget(self, window, callback, dndtype, event='', priority=50):

cmd = self._prepare_tkdnd_func(callback)

return self.tk.call('dnd', 'bindtarget', window, dndtype, event,

cmd, priority)

def bindtarget_query(self, window, dndtype=None, event=''):

return self.tk.call('dnd', 'bindtarget', window, dndtype, event)

def cleartarget(self, window):

self.tk.call('dnd', 'cleartarget', window)

def bindsource(self, window, callback, dndtype, priority=50):

cmd = self._prepare_tkdnd_func(callback)

self.tk.call('dnd', 'bindsource', window, dndtype, cmd, priority)

def bindsource_query(self, window, dndtype=None):

return self.tk.call('dnd', 'bindsource', window, dndtype)

def clearsource(self, window):

self.tk.call('dnd', 'clearsource', window)

def drag(self, window, actions=None, descriptions=None,

cursorwin=None, callback=None):

cmd = None

if cursorwin is not None:

if callback is not None:

cmd = self._prepare_tkdnd_func(callback)

self.tk.call('dnd', 'drag', window, actions, descriptions,

cursorwin, cmd)

_subst_format = ('%A', '%a', '%b', '%D', '%d', '%m', '%T',

'%W', '%X', '%Y', '%x', '%y')

_subst_format_str = " ".join(_subst_format)

def _prepare_tkdnd_func(self, callback):

funcid = self.master.register(callback, self._dndsubstitute)

cmd = ('%s %s' % (funcid, self._subst_format_str))

return cmd

def _dndsubstitute(self, *args):

if len(args) != len(self._subst_format):

return args

def try_int(x):

x = str(x)

try:

return int(x)

except ValueError:

return x

A, a, b, D, d, m, T, W, X, Y, x, y = args

event = Tkinter.Event()

event.action = A # Current action of the drag and drop operation.

event.action_list = a # Action list supported by the drag source.

event.mouse_button = b # Mouse button pressed during the drag and drop.

event.data = D # The data that has been dropped.

event.descr = d # The list of descriptions.

event.modifier = m # The list of modifier keyboard keys pressed.

event.dndtype = T

event.widget = self.master.nametowidget(W)

event.x_root = X # Mouse pointer x coord, relative to the root win.

event.y_root = Y

event.x = x # Mouse pointer x coord, relative to the widget.

event.y = y

event.action_list = str(event.action_list).split()

for name in ('mouse_button', 'x', 'y', 'x_root', 'y_root'):

setattr(event, name, try_int(getattr(event, name)))

return (event, )

与Tkdnd一起,您将发现一个tkdnd.tcl程序,它比它提供的C扩展更高一级。我没有包装这个高级代码,但是用Python复制它可能比使用这个低级包装器更有趣。

python资源管理器安装_python将资源管理器文件拖放到tkinter entry widg相关推荐

  1. python资源管理器安装_Python学习笔记-Python安装

    Python安装 文章简介:本文介绍在不同操作系统中搭建Python编程环境. 一 搭建编程环境 在不同的操作系统中,Python存在细微的区别,下面介绍两个主要的Python版本. 1.1 Pyth ...

  2. python pdb pip安装_Python调试器,一个优秀开发人员的必备技能包

    原标题:Python调试器,一个优秀开发人员的必备技能包 写在之前 不管是之前搞 acm 用 c/c++ 写算法还是后来用 Python 写代码,我发现在程序出现问题的时候,大多数人习惯性的用 pri ...

  3. python queue模块安装_Python queue包_程序模块 - PyPI - Python中文网

    沃特?另一个消息队列? 考虑到消息队列的激增,人们可能倾向于相信 发明更多不是答案.使用现有的解决方案是 多次尝试与大多数现有的消息队列产品. 其他的失败(对于我们的用例). queuey是用来处理大 ...

  4. python datetime需要安装_Python全栈工程师学习笔记 | Django的模型层

    Model模型 模型是你的数据的唯一的.权威的信息源.它包含你所储存数据的必要字段和行为. 通常,每个模型对应数据库中唯一的一张表. 每个模型都是django.db.models.Model的一个Py ...

  5. python cv2模块安装_Python运行脚本前,自动安装需要的模块包

    在服务器上部署Python程序时,往往需要先安装很多需要的模块包.如果一个一个安装就会出现忘记的情况.或者新增加某个新的模块时,也可能会忘记安装. 这里先讲一下怎么通过Python程序自动安装. 以下 ...

  6. python six库安装_Python 安装matplotlib,six,dateutil,pyparsing 完整过程

    因为matplotlib 需要依赖许多其他科学计算的第三方库,需要一个一个的安装了.. 1, 安装matplotlib 官网直接下载:http://matplotlib.sourceforge.net ...

  7. python image库安装_Python如何安装Image库呢?

    摘要: 下文讲述Python中安装Image库的方法分享,如下所示: 今天编写一个图片缩小工具, 出现以下错误提示信息 import Image ModuleNotFoundError: No mod ...

  8. python re库安装_python 库安装方法及常用库

    python库安装方法: 方法一:setpu.py 1.下载库压缩包,解压,记录下路径:*:/**/--/ 2.运行cmd,切换到*:/**/--/目录下 3.运行setup.py build 4.然 ...

  9. python pil怎么安装_python pil 怎么安装 怎样安装python的图像处理库pillow

    python 怎么安装pillow 1. 安装pip [plain] view plain copy sudo easy_install pip pip 安装成功就可以直接安装pil或者pillow ...

最新文章

  1. java api 设计_Java API设计实践
  2. simpack导入实际线路激励
  3. maven springboot 除去指定的jar包_1. Spring Boot概述
  4. 利用matlab将二进制小数转换为十进制小数
  5. ios 导航栏(自己定义和使用系统方式)
  6. linux 部署项目
  7. 洛谷2014 选课(树形DP)树形背包问题
  8. Python中什么是set
  9. Bert在CV领域的应用
  10. python面试题之用列表解析式选出1-100中的奇数
  11. 为什么都建议学java而不是python-为什么java比python复杂,还是有很多人选择学习java?...
  12. centos一键安装包无法创建vhost
  13. 好家伙!微软苏州 M365,上班时间打王者荣耀?
  14. Cannot define dimension expressions when an array initializer is provided 错误
  15. Android 高仿微信支付键盘
  16. 2023年的 苹果iPhone15/Pro 将有哪些变化
  17. (教程)教你如何自己办理商标注册事宜
  18. java-php-python-ssm幼儿园综合管理系统计算机毕业设计
  19. 一文带你快速上手正则表达式
  20. 百度Java面试题前200页。

热门文章

  1. vue SSR(开发)
  2. Zxing条码扫描二维码扫描简化
  3. 查看fna文件Linux,NCBI上基因组文件格式及解释
  4. 八卦与十二地支方位图_万物变化的经典,十二生肖八卦图方位图解析
  5. 字符编码ANSI和ASCII区别、Unicode和UTF-8区别
  6. strlen函数用法
  7. 非编系统它的主要功能是什么
  8. 访问交流 | 山东青岛城阳区副区长一行走访零数科技
  9. 广东扑克 之 锄大地
  10. 【陶晶驰串口屏】stm32h743驱动cubeide配置