|
4 | 4 |
|
5 | 5 | 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`. |
6 | 6 |
|
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 | + |
8 | 22 |
|
9 | 23 | ## Install |
10 | 24 |
|
@@ -34,27 +48,68 @@ root.mainloop() |
34 | 48 |
|
35 | 49 | see any of the [demos](./demos) for usage examples. |
36 | 50 |
|
37 | | -# tkinterdnd2 |
| 51 | +## Framework Integration |
38 | 52 |
|
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. |
40 | 54 |
|
41 | | -## What is TkinterDnD2 |
| 55 | +The following are some simple examples: |
42 | 56 |
|
43 | | -[TkinterDnD2](http://tkinterdnd.sourceforge.net) is a python wrapper for George Petasis' tkDnD Tk extension version 2. |
| 57 | +### PySimpleGUI |
44 | 58 |
|
45 | | -It is a domain public project. |
| 59 | +```python |
| 60 | +import PySimpleGUI as sg |
| 61 | +from tkinterdnd2 import TkinterDnD, DND_FILES |
46 | 62 |
|
47 | | -## What is TkDnD2 |
| 63 | +def on_drop(event): |
| 64 | + window["-FILE-"].update(event.data) |
48 | 65 |
|
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 | +] |
50 | 71 |
|
51 | | -## What this repository is about |
| 72 | +window = sg.Window("File Drop", layout, finalize=True) |
52 | 73 |
|
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) |
54 | 76 |
|
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) |
56 | 80 |
|
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 | +``` |
58 | 113 |
|
59 | 114 | ## pyinstaller |
60 | 115 |
|
|
0 commit comments