RobertJN64 / TKinterModernThemes

A collection of modern themes with code that makes it easy to integrate into a tkinter project.
MIT License
91 stars 7 forks source link

Drag&Drop function #7

Closed OlegXio closed 2 months ago

OlegXio commented 2 months ago

Is it possible to implement Drag and Drop function on TKMT as it is implemented in TkDND2 ? Example in TkDND2:

import tkinter as tk
from tkinterdnd2 import DND_FILES, TkinterDnD
def drop(event):
    entry_var.set(event.data)
root = TkinterDnD.Tk()
root.title('Drag and Drop Example')
entry_var = tk.StringVar()
entry = tk.Entry(root, textvariable=entry_var, width=50)
entry.pack(pady=20)
entry.drop_target_register(DND_FILES)
entry.dnd_bind('<<Drop>>', drop)
root.mainloop()
RobertJN64 commented 2 months ago

Sure - but it does require tying into 2 internal functions of the tkinterdnd2 library during setup.

Here is an example:

import TKinterModernThemes as TKMT
import tkinter as tk
from tkinterdnd2.TkinterDnD import DnDWrapper, _require
from tkinterdnd2 import DND_FILES

class App(TKMT.ThemedTKinterFrame, DnDWrapper):
    def __init__(self, theme, mode, usecommandlineargs=True, usethemeconfigfile=True):
        super().__init__("DnD", theme, mode, usecommandlineargs=usecommandlineargs,
                         useconfigfile=usethemeconfigfile)
        self.TkdndVersion = _require(self.root)

        self.dnd_frame = self.addLabelFrame("DnD Frame")
        entry_var = tk.StringVar()

        def drop(event):
            entry_var.set(event.data)

        entry = self.dnd_frame.Entry(entry_var)
        entry.drop_target_register(DND_FILES)
        entry.dnd_bind('<<Drop>>', drop)
        self.run()

if __name__ == "__main__":
    App("park", "dark")

The key is inheriting from DnDWrapper and calling self.TkdndVersion = _require(self.root). The label frame is optional - once DnD is setup the only requirement is creating an entry widget an binding the functions as normal.

Please let me know if you need anything else!

OlegXio commented 2 months ago

Thanks, it's working.