[ Enhancement ] A potential Drag and Drop solution on tkinter that's in the user code space. No PSG modification needed
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
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
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. 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
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di PySimpleGUI/PySimpleGUI
-
Bug Done - Install Dev Build (see docs for how) Platform Specific Issue - Linux Port - TK
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
PySimpleGUI/PySimpleGUI#6904 · 15 commenti ·
-
Bug Demo Programs Done - Install Dev Build (see docs for how) Interoperability Port - TK workaround available
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
PySimpleGUI/PySimpleGUI#6900 · 1 commento ·
-
Done - Install Dev Build (see docs for how) enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
PySimpleGUI/PySimpleGUI#3251 · 12 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 74/100
PySimpleGUI/PySimpleGUI#6906 · 1 commento · 1 reazione ·
-
Bug Done - Install Dev Build (see docs for how) Port - TK
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
PySimpleGUI/PySimpleGUI#6902 · 1 commento ·
Tutte le issue di PySimpleGUI/PySimpleGUI
Issue simili
-
essnmx good first issue
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
-
[Feature] 奇物选择添加优先级 Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
syfoud/Simulated_Scepter#174 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Giskard-AI/giskard-oss#2840 · 1 commento ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Apertaarea: repo bug perceived difficulty: 2
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
yeti-platform/yeti#1380 ·