1

I recently ran into this problem, and when I went searching for this question I didn't get any results back. I eventually found the answer to this question and learn enough to answer it myself.

I am building a modern tkinter GUI application with a drag and drop box. I want to use ttkbootstrap and tkinterdnd2 extensions.

Problem: Both extensions, ttkbootstrap and tkinterdnd2, seemingly need their own window to work as intended. Trying to inherit from both classes == Metaclass conflict

Intention: Put drag and drop capabilites into a ttkbootstrap.Window().

This is the code I ended with that worked. Proof of concept not final product.

import ttkbootstrap as bootstrap
import tkinter as tk
from tkinter import ttk
from ttkbootstrap.constants import SUCCESS
from tkinterdnd2 import DND_FILES, TkinterDnD
from tkinterdnd2.TkinterDnD import _require


class App(bootstrap.Window):
    def __init__(self):
        super().__init__()
        _require(self)  # unknown actions - REQUIRED for DnD functionality

        self.geometry("500x500")
        self.themename = SUCCESS

        dnd_frame = bootstrap.Frame(height=50, width=50)
        dnd_frame.pack()

        self.file_names_listbox = tk.Listbox(dnd_frame, selectmode=tk.SINGLE, background="light grey", height=100, width=100)
        self.file_names_listbox.pack(expand=True, padx=10, pady=20)
        self.file_names_listbox.drop_target_register(DND_FILES)
        self.file_names_listbox.dnd_bind("<<Drop>>", self.drop_inside_box)

        button = bootstrap.Button(self, text="button")
        button.pack(side='bottom', pady=(0, 10))

        self.mainloop()

    def drop_inside_box(self, event):
        self.file_names_listbox.insert('end', event.data)

if __name__ == "__main__":
    app = App()

Explanation: its all in the tkinterdnd2.TkinterDnD._require(). I did not dive into its roots, but once _require is called, drop_target_register() doesn't return an error, effectively giving DnD functionality WITHOUT a TkinterDnD window being initiated.

Conclusion: The super class is a ttkbootstrap.Window. This keeps all the theme-ability and GUI customability in ttkbootstrap and ttk widgets, and it has the drag and drop capability from tkinterdnd2.

MacOS Users: If you have arm64 tech, you will have to install tkinterdnd2-universal. Otherwise, you will get a runtime error.

To add, this gifted drag and drop functionality works with frames and other widgets as well, effectively making them have reactivity to files being dropped onto them.

Hope this helps those looking for answers.

Good luck, and keep coding!

Tristan
  • 19
  • 4
  • 1
    If you want to answer your own question, you should ask the question in the question section, and add the answer as an answer. Combining them in the question is confusing. – Bryan Oakley Jul 15 '23 at 20:13
  • I agree. I solved the problem well before deciding to add this to the discussion, so I didn't post with that intent. I appreciate the feedback though! – Tristan Jul 16 '23 at 02:40

0 Answers0