Skip to main content

TkinterDnD2 is a python wrapper for George Petasis'' tkDnD Tk extension version 2

Project description

TkinterDnD2

Eliav2/tkinterdnd2 is a fork of the (unmaintained) pmgagne/tkinterdnd2 which is a python wrapper for tkdnd .

This repo was originally forked and edited for the purpose of publishing to pypi so one could simply install this package with pip install tkinterdnd2.

This repository is being maintained to ensure availability of tkinterdnd2 into the future, providing Tkinter native drag and drop support for windows, unix and Mac OSX.

What is TkDnD

tkDnD2 is a tcl/Tk extension adding native drag and drop support.

This repository contains the compiled binaries from https://github.com/petasis/tkdnd/releases/tag/tkdnd-release-test-v2.9.5 and my own compiled binaries for full Tcl 9 support: https://github.com/Squiblydoo/tkdnd.

Install

python -m pip install tkinterdnd2

Usage

import tkinter as tk

from tkinterdnd2 import DND_FILES, TkinterDnD

root = TkinterDnD.Tk()  # notice - use this instead of tk.Tk()

lb = tk.Listbox(root)
lb.insert(1, "drag files to here")

# register the listbox as a drop target
lb.drop_target_register(DND_FILES)
lb.dnd_bind('<<Drop>>', lambda e: lb.insert(tk.END, e.data))

lb.pack()
root.mainloop()

tkinterdnd2 example usage

see any of the demos for usage examples.

Framework Integration

If you are using a GUI framework that manages its own Tk root window (such as PySimpleGUI or CustomTkinter), you cannot use TkinterDnD.Tk() as the root. Instead, call TkinterDnD.require() on the framework's existing root after it has been created. This loads tkdnd into the shared Tcl interpreter, making drag-and-drop available to all widgets in the process.

The following are some simple examples:

PySimpleGUI

import PySimpleGUI as sg
from tkinterdnd2 import TkinterDnD, DND_FILES

def on_drop(event):
    window["-FILE-"].update(event.data)

layout = [
    [sg.Text("Drag & Drop a File Here")],
    [sg.Input("", key="-FILE-")],
    [sg.Button("OK"), sg.Button("Cancel")],
]

window = sg.Window("File Drop", layout, finalize=True)

# Inject DnD into PySimpleGUI's own root — no dummy window needed
TkinterDnD.require(window.TKroot)

# Register any widget as a drop target
window["-FILE-"].widget.drop_target_register(DND_FILES)
window["-FILE-"].widget.dnd_bind("<<Drop>>", on_drop)

while True:
    event, values = window.read()
    if event in (sg.WIN_CLOSED, "Cancel"):
        break

window.close()

CustomTkinter

import customtkinter as ctk
from tkinterdnd2 import TkinterDnD, DND_FILES

def on_drop(event):
    entry.delete(0, "end")
    entry.insert(0, event.data)

app = ctk.CTk()
app.title("File Drop")

# Inject DnD into CustomTkinter's root
TkinterDnD.require(app)

entry = ctk.CTkEntry(app, width=400, placeholder_text="Drag a file here...")
entry.pack(padx=20, pady=20)

entry.drop_target_register(DND_FILES)
entry.dnd_bind("<<Drop>>", on_drop)

app.mainloop()

pyinstaller

When using pyinstaller, you should use the hook-tkinterdnd2.py file included to collect the TkDnD binaries and build them into the project. To use it, copy it into the base directory of your project, then run pyinstaller as follows:

pyinstaller -F -w myproject/myproject.py --additional-hooks-dir=.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkinterdnd2-0.6.0.tar.gz (678.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tkinterdnd2-0.6.0-py3-none-any.whl (753.5 kB view details)

Uploaded Python 3

File details

Details for the file tkinterdnd2-0.6.0.tar.gz.

File metadata

  • Download URL: tkinterdnd2-0.6.0.tar.gz
  • Upload date:
  • Size: 678.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for tkinterdnd2-0.6.0.tar.gz
Algorithm Hash digest
SHA256 bc20f84c25b39411062c50d18dd2af831faf399572ffad8f4e8316d6ff71af69
MD5 6bb569ea93da5b2c12f42228f1ce3f2b
BLAKE2b-256 899ca992d8e2a2222d3e94ae184d815b05af812803cf3caf6240b23941cd3d8b

See more details on using hashes here.

File details

Details for the file tkinterdnd2-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: tkinterdnd2-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 753.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for tkinterdnd2-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f583b5cae69774344ca22add6efcb68add32a73c80b667db9c9befee73f7ee27
MD5 23fb870fcb2816a344baaf2ee358d760
BLAKE2b-256 2f99ced2c17a4f490fbf2b126743c713156842b174310937713927c3c6ad4a70

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page