rdbende / tkinterdnd Goto Github PK
View Code? Open in Web Editor NEWA nice and easy-to-use wrapper around the tkdnd package
Home Page: https://pypi.org/project/python-tkdnd
License: MIT License
A nice and easy-to-use wrapper around the tkdnd package
Home Page: https://pypi.org/project/python-tkdnd
License: MIT License
When a modal window displays (e.g. messagebox) whilst the parent window widgets are disabled, it is still possible to dnd into the parent window.
Attached is your example from the PyPi website. I have added two buttons:
Invoke the messagebox, now drag filenames to your dnd enabled area. DnD does not comply with the behaviour of other tk widgets, which are disabled by the invocation of the modal (messagebox) window.
Kind regards
Issue with DnD and modal windows.txt
When window is binded to <<Drop>>>
and a file is dropped on it shows no dropping icon. The function that was binded also doesn't run
root = Tk()
root.register_drop_target("*")
root.bind("<<Drop>>", print)
root.mainloop()
After dragging a file onto the window, I got this:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/tkinter/__init__.py", line 1947, in __call__
args = self.subst(*args)
^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/tkinter/__init__.py", line 1632, in _substitute
e.serial = getint(nsign)
^^^^^^^^^^^^^
_tkinter.TclError: expected integer but got "%#"
(Originally posted in python/cpython, closed there and opened here because of using this repo's library.)
Handling of a Drag-n-Drop event with tkinter causes an exception:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1920, in __call__
args = self.subst(*args)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1605, in _substitute
e.serial = getint(nsign)
_tkinter.TclError: expected integer but got "%#"
The basic code used is this (more code is in the app, but this should be the relevant parts of the setup):
def drop(event):
print("DROP EVENT:", event) # test code to see what's delivered
import tkinterDnD
root = tkinterDnD.Tk()
root.register_drop_target("*")
root.bind("<<Drop>>", drop)
The intent is to capture a file dragged from another application. With the 'register' function, this Python program and the OS indicate that the program's window is a valid place to drop a file dragged from a file manager program (specifically using Windows Explorer). After 'dropping', the error shown above is seen.
Environment:
CPython versions tested on: Python.3.10_3.10.1520.0_x64__qbz5n2kfra8p0
Operating system and architecture: Windows 10
Hi there,
The example given on the main page doesn't work anymore. Python 3.11
Any reasons why? Installed right the module..
Try version 2.8 from SOURCEFORGE if you run into errors with version 2.9.2.
I installed using pip, had issues, so I downgraded to 2.8 by replacing the file contents of version 2.9.2 with those from 2.8, but I kept utils from 2.9.2 in the install location - no errors after that and drag and drop worked fine.
I will expand upon this further at a later time with more descriptive info and file locations.
Just brief for now.
Edit:
pip install python-tkdnd | known as: python-tkdnd 0.2.1 (~ https://pypi.org/project/python-tkdnd/ ~) | but this installed: tkinterDnD 2.9.2 | but this version was causing trouble; errors and such, so I manually replaced it with version 2.8 that was manually downloaded from SOURCEFORGE: (tkdnd2.8-win32-ix86.tar.gz) ~ https://sourceforge.net/projects/tkdnd/files/ ~, but tkdnd_utils.tcl from 2.9.2 was kept by me to stay with the 2.8 replacement version though and left in its directory. %USERPROFILE%\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\tkinterDnD\windows | This is the location where the files were replaced. My application's drag and drop is now working. import tkinter as tk; from tkinter import ttk, filedialog; import tkinterDnD. Just wanted to share this in case anyone else found themselves experiencing problems.
If I can find my history of any of the actual errors that occurred at the time with version 2.9.2, I'll edit this again and include those here as well.
Edit2:
Error section:
TkDnDVersion = master.tk.call('package', 'require', 'tkdnd')
_tkinter.TclError: couldn't load library "%USERPROFILE%\AppData\Local\Programs\Python\Python36-32\lib\site-packages\tkinterDnD\windows/libtkdnd2.9.2.dll"
So, it seemed that there was an issue with loading the libtkdnd2.9.2.dll dynamic link library file which is required by TkinterDnD. This error indicated to me that that the DLL file could not be loaded, which that likely caused the TkDnDVersion initialisation to fail.
AND I THINK I HAVE JUST CRACKED WHY - POTENTIALLY VERSION 2.9.2 MAY HAVE TYPO'D THE FOLLOWING FOLDER:
Correct:
Python\Python36-32\Lib\site-packages\tkinterDnD
Incorrect:
Python\Python36-32\lib\site-packages\tkinterDnD
It's very subtle but in the directory: Python36-32
There are two folders:
One named: libs
And one named: Lib
And the latter folder has a CAPITAL, L - and maybe the DLL failed to load for me due to version 2.9.2 having made a typo and used a lower case, l.
This could explain the error and why it could not load the DLL for me, but I cannot confirm this though. I am speculating.
Resources I've been to; random list:
https://pypi.org/project/python-tkdnd/
https://sourceforge.net/projects/tkdnd/files/
https://sourceforge.net/projects/tkdnd/
https://github.com/petasis/tkdnd
https://github.com/petasis/tkdnd/issues
https://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html
https://docs.python.org/3/library/tkinter.dnd.html
https://github.com/rdbende/tkinterDnD
Action | GNOME | Plasma | Windows | macOS |
---|---|---|---|---|
Text dropping | ✅ | ✅ | ✅ | ❔ |
Text dragging | ✅ | ✅ | ✅ | ❔ |
File dropping | ✅ | ✅ | ✅ | ❔ |
File dragging | ✅ | ✅ | ✅ | ❔ |
Color dropping | ✅ | ✅ | ❌ | ❔ |
Color dragging | ✅ | ✅ | ❌ | ❔ |
HTML dropping | ✅ | ✅ | ❔ | ❔ |
Creating a hook with ttkwidgets.hook
so that you don't have to write an entire widget like this, but you can easily handle the dnd events specified with arguments
class DnDFrame(tk.Frame):
def __init__(self, *args, **kwargs):
onfiledrop = kwargs.pop("onfiledrop", None)
ontextdrop = kwargs.pop("ontextdrop", None)
ondrop = kwargs.pop("ondrop", None)
ondragstart = kwargs.pop("ondragstart", None)
ondragend = kwargs.pop("ondragend", None)
ondragenter = kwargs.pop("onadragenter", None)
ondragleave = kwargs.pop("ondragleave", None)
ondragmove = kwargs.pop("ondragmove", None)
tk.Frame.__init__(self, *args, **kwargs)
if onfiledrop:
self.bind("<<Drop:DND_Files>>", onfiledrop)
self.register_drop_target("DND_Files")
if ontextdrop:
self.bind("<<Drop:DND_Text>>", ontextdrop)
self.register_drop_target("DND_Text")
if ondrop:
self.bind("<<Drop>>", ondrop)
self.register_drop_target("*")
if ondragstart:
self.bind("<<DragInitCmd>>", ondragstart)
self.register_drag_source()
if ondragend:
self.bind("<<DragEndCmd>>", ondragend)
self.register_drag_source()
if ondragenter:
self.bind("<<DropEnter>>", ondragenter)
self.register_drop_target("*")
if ondragleave:
self.bind("<<DropLeave>>", ondragleave)
self.register_drop_target("*")
if ondragmove:
self.bind("<<DropPosition>>", ondragmove)
self.register_drop_target("*")
def bind(self, sequence=None, func=None, add=None):
"""
I want to handle simple events like <<Drop:File>> instead of
<<Drop:DND_Files>>, but I can't do it with `event_add`,
so I'm rewriting the bind
"""
if sequence == "<<Drop:File>>":
sequence = "<<Drop:DND_Files>>"
elif sequence == "<<Drop:Text>>":
sequence = "<<Drop:DND_Files>>"
elif sequence == "<<Drop:Any>>":
sequence = "<<Drop>>"
return tk.Frame.bind(self, sequence, func, add)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.