Skip to content

Commit b4cb9e8

Browse files
committed
Improve Tk import
These build adds an explicit means of adding TkinterDnd to an existing TK window and maintains backwards compatibility with the previous implied import.
1 parent 71e0c68 commit b4cb9e8

3 files changed

Lines changed: 87 additions & 14 deletions

File tree

README.md

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@
44

55
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`.
66

7-
This repository is being maintained to ensure availability of `tkinterdnd2` into the future.
7+
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.
8+
9+
10+
### What is TkDnD2
11+
12+
[tkDnD2](https://github.com/petasis/tkdnd) is a tcl/Tk extension adding native drag and drop support.
13+
14+
### What this repository is about
15+
16+
This repo package TkinterDnD2 and tkdnd2 into a standard python module.
17+
18+
When the extension is imported in python its location will be automatically added to the Tk search path.
19+
20+
This repository contains the compiled binaries from https://github.com/petasis/tkdnd/releases/tag/tkdnd-release-test-v2.9.4. In order to provide support on ARM, we include built binaries from the now defunct [tkinterdnd2-universal](https://pypi.org/project/tkinterdnd2-universal/#files) which added ARM support.
21+
822

923
## Install
1024

@@ -34,27 +48,68 @@ root.mainloop()
3448

3549
see any of the [demos](./demos) for usage examples.
3650

37-
# tkinterdnd2
51+
## Framework Integration
3852

39-
Tkinter native drag and drop support for windows, unix and Mac OSX.
53+
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.
4054

41-
## What is TkinterDnD2
55+
The following are some simple examples:
4256

43-
[TkinterDnD2](http://tkinterdnd.sourceforge.net) is a python wrapper for George Petasis' tkDnD Tk extension version 2.
57+
### PySimpleGUI
4458

45-
It is a domain public project.
59+
```python
60+
import PySimpleGUI as sg
61+
from tkinterdnd2 import TkinterDnD, DND_FILES
4662

47-
## What is TkDnD2
63+
def on_drop(event):
64+
window["-FILE-"].update(event.data)
4865

49-
[tkDnD2](https://github.com/petasis/tkdnd) is a tcl/Tk extension adding native drag and drop support.
66+
layout = [
67+
[sg.Text("Drag & Drop a File Here")],
68+
[sg.Input("", key="-FILE-")],
69+
[sg.Button("OK"), sg.Button("Cancel")],
70+
]
5071

51-
## What this repository is about
72+
window = sg.Window("File Drop", layout, finalize=True)
5273

53-
This repo package TkinterDnD2 and tkdnd2 into a standard python module.
74+
# Inject DnD into PySimpleGUI's own root — no dummy window needed
75+
TkinterDnD.require(window.TKroot)
5476

55-
When the extension is imported in python its location will be automatically added to the Tk search path.
77+
# Register any widget as a drop target
78+
window["-FILE-"].widget.drop_target_register(DND_FILES)
79+
window["-FILE-"].widget.dnd_bind("<<Drop>>", on_drop)
5680

57-
This repository contains the compiled binaries from https://github.com/petasis/tkdnd/releases/tag/tkdnd-release-test-v2.9.4. In order to provide support on ARM, we include built binaries from the now defunct [tkinterdnd2-universal](https://pypi.org/project/tkinterdnd2-universal/#files) which added ARM support.
81+
while True:
82+
event, values = window.read()
83+
if event in (sg.WIN_CLOSED, "Cancel"):
84+
break
85+
86+
window.close()
87+
```
88+
89+
### CustomTkinter
90+
91+
```python
92+
import customtkinter as ctk
93+
from tkinterdnd2 import TkinterDnD, DND_FILES
94+
95+
def on_drop(event):
96+
entry.delete(0, "end")
97+
entry.insert(0, event.data)
98+
99+
app = ctk.CTk()
100+
app.title("File Drop")
101+
102+
# Inject DnD into CustomTkinter's root
103+
TkinterDnD.require(app)
104+
105+
entry = ctk.CTkEntry(app, width=400, placeholder_text="Drag a file here...")
106+
entry.pack(padx=20, pady=20)
107+
108+
entry.drop_target_register(DND_FILES)
109+
entry.dnd_bind("<<Drop>>", on_drop)
110+
111+
app.mainloop()
112+
```
58113

59114
## pyinstaller
60115

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
setuptools.setup(
77
name="tkinterdnd2",
8-
version="0.4.4.1",
9-
author="petasis\\pmgagne\\eliav2",
8+
version="0.5.0",
9+
author="petasis\\pmgagne\\eliav2\\squiblydoo",
1010
description="TkinterDnD2 is a python wrapper for George Petasis'' tkDnD Tk extension version 2",
1111
long_description=long_description,
1212
long_description_content_type="text/markdown",

tkinterdnd2/TkinterDnD.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,24 @@ def set_dropfile_tempdir(self, tempdir):
293293
#### themselves and all their descendant widgets: ####
294294
#######################################################################
295295

296+
def require(tkroot):
297+
"""Enable tkdnd drag-and-drop on an existing Tk root.
298+
299+
Use this when your application uses a GUI framework (e.g. PySimpleGUI,
300+
CustomTkinter) that manages its own Tk root and you cannot use
301+
TkinterDnD.Tk() as the root window.
302+
303+
Call this after the framework has created and finalised its window::
304+
305+
window = sg.Window('Title', layout, finalize=True)
306+
TkinterDnD.require(window.TKroot)
307+
308+
After this call, any widget in the process can be registered as a
309+
drop target via widget.drop_target_register() and widget.dnd_bind().
310+
"""
311+
return _require(tkroot)
312+
313+
296314
class Tk(tkinter.Tk, DnDWrapper):
297315
'''Creates a new instance of a tkinter.Tk() window; all methods of the
298316
DnDWrapper class apply to this window and all its descendants.'''

0 commit comments

Comments
 (0)