के पास इसे संभालने के लिए कोई आदेश नहीं है, और पाइथन में ड्रैग & ड्रॉप-इंटर-एप्लिकेशन करने के लिए कोई अतिरिक्त अतिरिक्त एक्सटेंशन शामिल नहीं है, इसलिए आपको ऑपरेशन करने के लिए एक एक्सटेंशन की आवश्यकता है। Tkdnd (http://sourceforge.net/projects/tkdnd पर विस्तार, Tkdnd.py मॉड्यूल नहीं) मेरे लिए काम करता है। इसे पायथन से उपयोग करने के लिए, एक रैपर की आवश्यकता होती है। एक के लिए जल्दी खोज रहे हैं, ऐसा लगता है कि http://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html में ऐसा कोड है। मैंने एक और किया क्योंकि मुझे वह दूसरा पसंद नहीं आया। मेरे रैपर के साथ समस्या यह है कि यह बेहद अवांछित है, वास्तव में मैंने केवल bindtarget
फ़ंक्शन का उपयोग किया और केवल 10 सेकंड या उससे भी अधिक के लिए।
नीचे रैपर के साथ, आप कुछ विजेट बना सकते हैं और घोषणा कर सकते हैं कि यह ड्रैग की गई फ़ाइलों को प्राप्त करने का समर्थन करता है।
# The next two lines are not necessary if you installed TkDnd
# in a proper place.
import os
os.environ['TKDND_LIBRARY'] = DIRECTORYTOTHETKDNDBINARY
import Tkinter
from untested_tkdnd_wrapper import TkDND
root = Tkinter.Tk()
dnd = TkDND(root)
entry = Tkinter.Entry()
entry.pack()
def handle(event):
event.widget.insert(0, event.data)
dnd.bindtarget(entry, handle, 'text/uri-list')
root.mainloop()
यहाँ और untested_tkdnd_wrapper.py
के लिए कोड है:: यहाँ एक उदाहरण है
import os
import Tkinter
def _load_tkdnd(master):
tkdndlib = os.environ.get('TKDND_LIBRARY')
if tkdndlib:
master.tk.eval('global auto_path; lappend auto_path {%s}' % tkdndlib)
master.tk.eval('package require tkdnd')
master._tkdnd_loaded = True
class TkDND(object):
def __init__(self, master):
if not getattr(master, '_tkdnd_loaded', False):
_load_tkdnd(master)
self.master = master
self.tk = master.tk
# Available pre-defined values for the 'dndtype' parameter:
# text/plain
# text/plain;charset=UTF-8
# text/uri-list
def bindtarget(self, window, callback, dndtype, event='<Drop>', priority=50):
cmd = self._prepare_tkdnd_func(callback)
return self.tk.call('dnd', 'bindtarget', window, dndtype, event,
cmd, priority)
def bindtarget_query(self, window, dndtype=None, event='<Drop>'):
return self.tk.call('dnd', 'bindtarget', window, dndtype, event)
def cleartarget(self, window):
self.tk.call('dnd', 'cleartarget', window)
def bindsource(self, window, callback, dndtype, priority=50):
cmd = self._prepare_tkdnd_func(callback)
self.tk.call('dnd', 'bindsource', window, dndtype, cmd, priority)
def bindsource_query(self, window, dndtype=None):
return self.tk.call('dnd', 'bindsource', window, dndtype)
def clearsource(self, window):
self.tk.call('dnd', 'clearsource', window)
def drag(self, window, actions=None, descriptions=None,
cursorwin=None, callback=None):
cmd = None
if cursorwin is not None:
if callback is not None:
cmd = self._prepare_tkdnd_func(callback)
self.tk.call('dnd', 'drag', window, actions, descriptions,
cursorwin, cmd)
_subst_format = ('%A', '%a', '%b', '%D', '%d', '%m', '%T',
'%W', '%X', '%Y', '%x', '%y')
_subst_format_str = " ".join(_subst_format)
def _prepare_tkdnd_func(self, callback):
funcid = self.master.register(callback, self._dndsubstitute)
cmd = ('%s %s' % (funcid, self._subst_format_str))
return cmd
def _dndsubstitute(self, *args):
if len(args) != len(self._subst_format):
return args
def try_int(x):
x = str(x)
try:
return int(x)
except ValueError:
return x
A, a, b, D, d, m, T, W, X, Y, x, y = args
event = Tkinter.Event()
event.action = A # Current action of the drag and drop operation.
event.action_list = a # Action list supported by the drag source.
event.mouse_button = b # Mouse button pressed during the drag and drop.
event.data = D # The data that has been dropped.
event.descr = d # The list of descriptions.
event.modifier = m # The list of modifier keyboard keys pressed.
event.dndtype = T
event.widget = self.master.nametowidget(W)
event.x_root = X # Mouse pointer x coord, relative to the root win.
event.y_root = Y
event.x = x # Mouse pointer x coord, relative to the widget.
event.y = y
event.action_list = str(event.action_list).split()
for name in ('mouse_button', 'x', 'y', 'x_root', 'y_root'):
setattr(event, name, try_int(getattr(event, name)))
return (event,)
Tkdnd के साथ, आप एक tkdnd.tcl
प्रोग्राम है जो खुद सी विस्तार यह प्रदान करता है से अधिक एक उच्च स्तर है मिल जाएगा। मैंने इस उच्च स्तर के कोड को लपेट नहीं लिया है, लेकिन यह निचले स्तर के आवरण का उपयोग करने के बजाय पायथन में इसे दोहराने के लिए और अधिक दिलचस्प हो सकता है।
वास्तव में, 'Tkdnd' मॉड्यूल नहीं है लेकिन यह केवल एप्लिकेशन के अंदर काम करता है (यानी, एप्लिकेशन द्वारा बनाई गई विंडो में खींचें और छोड़ें)। एक विस्तार विस्तार है जिसे TkDND (http://sourceforge.net/projects/tkdnd/) कहा जाता है जो आप चाहते हैं कि इसका समर्थन करता है, इसके लिए पाइथन बाध्यकारी बनाना (यदि कोई नहीं है) आसान होना चाहिए। – mmgp