Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[ Enhancement ] A potential Drag and Drop solution on tkinter that's in the user code space. No PSG modification needed

Aperta
#6,891 13 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
desktop

Direzione di ricerca

Inizia eseguendo la riproduzione fornita su Windows con Python 3.11 o 3.14, PySimpleGUI 6.1.6, tkinter e tkinterdnd2. Esamina l’issue di tkinterdnd2 collegata nel report e verifica se la finestra dummy nascosta e il widget Input registrato funzionano in modo affidabile. Il report non definisce una modifica al repository né una condizione chiara di completamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Demo Programs documentation Done - Install Dev Build (see docs for how) enhancement Port - TK
Type of Issue (Enhancement, Error, Bug, Question)

Enhancement


Operating System

Tested on Windows so far

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions

Python version (sg.sys.version)

Tested on 3.11 & 3.14

PySimpleGUI Version (sg.__version__)

6.1.6

In theory this hack should work with almost any version of PySimpleGUI

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)

Tested on 8.6.12 & 8.6.15


Detailed Description

Drag and drop has been something we've wanted on tkinter for some time, but I hadn't found a way to do it that didn't involve pulling more packages/code into PySimpleGUI itself. I might have found a way to do it entirely in the user code space. It looks really sketchy, but does seem to work.

I created a dummy window using the tkinterdnd2 package, hide the window, then created a PySimpleGUI window. After the window is finalized, I registered the Input Element's widget as a drop target and set a callback function.

I opened an issue in the tkinterdnd2 GitHub repo (the one for the matching PyPI package) to see if this a reasonable technique or if I just got lucky in a narrow use case. It would be awesome if it's this simple to add drag and drop to a tkinter PySimpleGUI application. Image Hoping to hear back that it'll work.

Code To Duplicate
import PySimpleGUI as sg
from tkinterdnd2 import TkinterDnD, DND_FILES

#----------------------- tkinterdnd2 specific -------------------------------------#

def dnd_init():
    # Make a TkinterDnD dummy window and hide it
    root = TkinterDnD.Tk()
    root.attributes('-alpha', 0)
    root.withdraw()
    dnd_init.root = root

def dnd_exit():
    dnd_init.root.destroy()

def register_element_dnd(element:sg.Element):
    element.widget.drop_target_register(DND_FILES)
    element.widget.dnd_bind("<<Drop>>", on_drop)

#-------------------------PySimpleGUI code ----------------------------------------#


def on_drop(event):
    # Called when a drop event happens
    filepath = event.data.replace("{", "").replace("}", "")    # remove {}  filenames with spaces are in format "{filename with spaces}"
    window["-FILE-"].update(filepath)


dnd_init()

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

# layout = [[sg.Column(layout, p=0, k='-COL-')]]        # if want to make the entire window the target

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

register_element_dnd(window['-FILE-'])          # register the Input element as the target
# register_element_dnd(window['-COL-'])         # register the column element that contains the entire window as the target

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

window.close()

dnd_exit()          # being a good citizen and cleaning up the dummy window. May be able to get away with not destroying it

Screenshot, Sketch, or Drawing
Image
Lingua principale
Python
Stelle
13.8k
Fork
1.8k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PySimpleGUI/PySimpleGUI

Tutte le issue di PySimpleGUI/PySimpleGUI

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.