Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../tkinter
File: dnd.py
"""Drag-and-drop support for Tkinter.
[0] Fix | Delete
[1] Fix | Delete
This is very preliminary. I currently only support dnd *within* one
[2] Fix | Delete
application, between different windows (or within the same window).
[3] Fix | Delete
[4] Fix | Delete
I am trying to make this as generic as possible -- not dependent on
[5] Fix | Delete
the use of a particular widget or icon type, etc. I also hope that
[6] Fix | Delete
this will work with Pmw.
[7] Fix | Delete
[8] Fix | Delete
To enable an object to be dragged, you must create an event binding
[9] Fix | Delete
for it that starts the drag-and-drop process. Typically, you should
[10] Fix | Delete
bind <ButtonPress> to a callback function that you write. The function
[11] Fix | Delete
should call Tkdnd.dnd_start(source, event), where 'source' is the
[12] Fix | Delete
object to be dragged, and 'event' is the event that invoked the call
[13] Fix | Delete
(the argument to your callback function). Even though this is a class
[14] Fix | Delete
instantiation, the returned instance should not be stored -- it will
[15] Fix | Delete
be kept alive automatically for the duration of the drag-and-drop.
[16] Fix | Delete
[17] Fix | Delete
When a drag-and-drop is already in process for the Tk interpreter, the
[18] Fix | Delete
call is *ignored*; this normally averts starting multiple simultaneous
[19] Fix | Delete
dnd processes, e.g. because different button callbacks all
[20] Fix | Delete
dnd_start().
[21] Fix | Delete
[22] Fix | Delete
The object is *not* necessarily a widget -- it can be any
[23] Fix | Delete
application-specific object that is meaningful to potential
[24] Fix | Delete
drag-and-drop targets.
[25] Fix | Delete
[26] Fix | Delete
Potential drag-and-drop targets are discovered as follows. Whenever
[27] Fix | Delete
the mouse moves, and at the start and end of a drag-and-drop move, the
[28] Fix | Delete
Tk widget directly under the mouse is inspected. This is the target
[29] Fix | Delete
widget (not to be confused with the target object, yet to be
[30] Fix | Delete
determined). If there is no target widget, there is no dnd target
[31] Fix | Delete
object. If there is a target widget, and it has an attribute
[32] Fix | Delete
dnd_accept, this should be a function (or any callable object). The
[33] Fix | Delete
function is called as dnd_accept(source, event), where 'source' is the
[34] Fix | Delete
object being dragged (the object passed to dnd_start() above), and
[35] Fix | Delete
'event' is the most recent event object (generally a <Motion> event;
[36] Fix | Delete
it can also be <ButtonPress> or <ButtonRelease>). If the dnd_accept()
[37] Fix | Delete
function returns something other than None, this is the new dnd target
[38] Fix | Delete
object. If dnd_accept() returns None, or if the target widget has no
[39] Fix | Delete
dnd_accept attribute, the target widget's parent is considered as the
[40] Fix | Delete
target widget, and the search for a target object is repeated from
[41] Fix | Delete
there. If necessary, the search is repeated all the way up to the
[42] Fix | Delete
root widget. If none of the target widgets can produce a target
[43] Fix | Delete
object, there is no target object (the target object is None).
[44] Fix | Delete
[45] Fix | Delete
The target object thus produced, if any, is called the new target
[46] Fix | Delete
object. It is compared with the old target object (or None, if there
[47] Fix | Delete
was no old target widget). There are several cases ('source' is the
[48] Fix | Delete
source object, and 'event' is the most recent event object):
[49] Fix | Delete
[50] Fix | Delete
- Both the old and new target objects are None. Nothing happens.
[51] Fix | Delete
[52] Fix | Delete
- The old and new target objects are the same object. Its method
[53] Fix | Delete
dnd_motion(source, event) is called.
[54] Fix | Delete
[55] Fix | Delete
- The old target object was None, and the new target object is not
[56] Fix | Delete
None. The new target object's method dnd_enter(source, event) is
[57] Fix | Delete
called.
[58] Fix | Delete
[59] Fix | Delete
- The new target object is None, and the old target object is not
[60] Fix | Delete
None. The old target object's method dnd_leave(source, event) is
[61] Fix | Delete
called.
[62] Fix | Delete
[63] Fix | Delete
- The old and new target objects differ and neither is None. The old
[64] Fix | Delete
target object's method dnd_leave(source, event), and then the new
[65] Fix | Delete
target object's method dnd_enter(source, event) is called.
[66] Fix | Delete
[67] Fix | Delete
Once this is done, the new target object replaces the old one, and the
[68] Fix | Delete
Tk mainloop proceeds. The return value of the methods mentioned above
[69] Fix | Delete
is ignored; if they raise an exception, the normal exception handling
[70] Fix | Delete
mechanisms take over.
[71] Fix | Delete
[72] Fix | Delete
The drag-and-drop processes can end in two ways: a final target object
[73] Fix | Delete
is selected, or no final target object is selected. When a final
[74] Fix | Delete
target object is selected, it will always have been notified of the
[75] Fix | Delete
potential drop by a call to its dnd_enter() method, as described
[76] Fix | Delete
above, and possibly one or more calls to its dnd_motion() method; its
[77] Fix | Delete
dnd_leave() method has not been called since the last call to
[78] Fix | Delete
dnd_enter(). The target is notified of the drop by a call to its
[79] Fix | Delete
method dnd_commit(source, event).
[80] Fix | Delete
[81] Fix | Delete
If no final target object is selected, and there was an old target
[82] Fix | Delete
object, its dnd_leave(source, event) method is called to complete the
[83] Fix | Delete
dnd sequence.
[84] Fix | Delete
[85] Fix | Delete
Finally, the source object is notified that the drag-and-drop process
[86] Fix | Delete
is over, by a call to source.dnd_end(target, event), specifying either
[87] Fix | Delete
the selected target object, or None if no target object was selected.
[88] Fix | Delete
The source object can use this to implement the commit action; this is
[89] Fix | Delete
sometimes simpler than to do it in the target's dnd_commit(). The
[90] Fix | Delete
target's dnd_commit() method could then simply be aliased to
[91] Fix | Delete
dnd_leave().
[92] Fix | Delete
[93] Fix | Delete
At any time during a dnd sequence, the application can cancel the
[94] Fix | Delete
sequence by calling the cancel() method on the object returned by
[95] Fix | Delete
dnd_start(). This will call dnd_leave() if a target is currently
[96] Fix | Delete
active; it will never call dnd_commit().
[97] Fix | Delete
[98] Fix | Delete
"""
[99] Fix | Delete
[100] Fix | Delete
[101] Fix | Delete
import tkinter
[102] Fix | Delete
[103] Fix | Delete
[104] Fix | Delete
# The factory function
[105] Fix | Delete
[106] Fix | Delete
def dnd_start(source, event):
[107] Fix | Delete
h = DndHandler(source, event)
[108] Fix | Delete
if h.root:
[109] Fix | Delete
return h
[110] Fix | Delete
else:
[111] Fix | Delete
return None
[112] Fix | Delete
[113] Fix | Delete
[114] Fix | Delete
# The class that does the work
[115] Fix | Delete
[116] Fix | Delete
class DndHandler:
[117] Fix | Delete
[118] Fix | Delete
root = None
[119] Fix | Delete
[120] Fix | Delete
def __init__(self, source, event):
[121] Fix | Delete
if event.num > 5:
[122] Fix | Delete
return
[123] Fix | Delete
root = event.widget._root()
[124] Fix | Delete
try:
[125] Fix | Delete
root.__dnd
[126] Fix | Delete
return # Don't start recursive dnd
[127] Fix | Delete
except AttributeError:
[128] Fix | Delete
root.__dnd = self
[129] Fix | Delete
self.root = root
[130] Fix | Delete
self.source = source
[131] Fix | Delete
self.target = None
[132] Fix | Delete
self.initial_button = button = event.num
[133] Fix | Delete
self.initial_widget = widget = event.widget
[134] Fix | Delete
self.release_pattern = "<B%d-ButtonRelease-%d>" % (button, button)
[135] Fix | Delete
self.save_cursor = widget['cursor'] or ""
[136] Fix | Delete
widget.bind(self.release_pattern, self.on_release)
[137] Fix | Delete
widget.bind("<Motion>", self.on_motion)
[138] Fix | Delete
widget['cursor'] = "hand2"
[139] Fix | Delete
[140] Fix | Delete
def __del__(self):
[141] Fix | Delete
root = self.root
[142] Fix | Delete
self.root = None
[143] Fix | Delete
if root:
[144] Fix | Delete
try:
[145] Fix | Delete
del root.__dnd
[146] Fix | Delete
except AttributeError:
[147] Fix | Delete
pass
[148] Fix | Delete
[149] Fix | Delete
def on_motion(self, event):
[150] Fix | Delete
x, y = event.x_root, event.y_root
[151] Fix | Delete
target_widget = self.initial_widget.winfo_containing(x, y)
[152] Fix | Delete
source = self.source
[153] Fix | Delete
new_target = None
[154] Fix | Delete
while target_widget:
[155] Fix | Delete
try:
[156] Fix | Delete
attr = target_widget.dnd_accept
[157] Fix | Delete
except AttributeError:
[158] Fix | Delete
pass
[159] Fix | Delete
else:
[160] Fix | Delete
new_target = attr(source, event)
[161] Fix | Delete
if new_target:
[162] Fix | Delete
break
[163] Fix | Delete
target_widget = target_widget.master
[164] Fix | Delete
old_target = self.target
[165] Fix | Delete
if old_target is new_target:
[166] Fix | Delete
if old_target:
[167] Fix | Delete
old_target.dnd_motion(source, event)
[168] Fix | Delete
else:
[169] Fix | Delete
if old_target:
[170] Fix | Delete
self.target = None
[171] Fix | Delete
old_target.dnd_leave(source, event)
[172] Fix | Delete
if new_target:
[173] Fix | Delete
new_target.dnd_enter(source, event)
[174] Fix | Delete
self.target = new_target
[175] Fix | Delete
[176] Fix | Delete
def on_release(self, event):
[177] Fix | Delete
self.finish(event, 1)
[178] Fix | Delete
[179] Fix | Delete
def cancel(self, event=None):
[180] Fix | Delete
self.finish(event, 0)
[181] Fix | Delete
[182] Fix | Delete
def finish(self, event, commit=0):
[183] Fix | Delete
target = self.target
[184] Fix | Delete
source = self.source
[185] Fix | Delete
widget = self.initial_widget
[186] Fix | Delete
root = self.root
[187] Fix | Delete
try:
[188] Fix | Delete
del root.__dnd
[189] Fix | Delete
self.initial_widget.unbind(self.release_pattern)
[190] Fix | Delete
self.initial_widget.unbind("<Motion>")
[191] Fix | Delete
widget['cursor'] = self.save_cursor
[192] Fix | Delete
self.target = self.source = self.initial_widget = self.root = None
[193] Fix | Delete
if target:
[194] Fix | Delete
if commit:
[195] Fix | Delete
target.dnd_commit(source, event)
[196] Fix | Delete
else:
[197] Fix | Delete
target.dnd_leave(source, event)
[198] Fix | Delete
finally:
[199] Fix | Delete
source.dnd_end(target, event)
[200] Fix | Delete
[201] Fix | Delete
[202] Fix | Delete
# ----------------------------------------------------------------------
[203] Fix | Delete
# The rest is here for testing and demonstration purposes only!
[204] Fix | Delete
[205] Fix | Delete
class Icon:
[206] Fix | Delete
[207] Fix | Delete
def __init__(self, name):
[208] Fix | Delete
self.name = name
[209] Fix | Delete
self.canvas = self.label = self.id = None
[210] Fix | Delete
[211] Fix | Delete
def attach(self, canvas, x=10, y=10):
[212] Fix | Delete
if canvas is self.canvas:
[213] Fix | Delete
self.canvas.coords(self.id, x, y)
[214] Fix | Delete
return
[215] Fix | Delete
if self.canvas:
[216] Fix | Delete
self.detach()
[217] Fix | Delete
if not canvas:
[218] Fix | Delete
return
[219] Fix | Delete
label = tkinter.Label(canvas, text=self.name,
[220] Fix | Delete
borderwidth=2, relief="raised")
[221] Fix | Delete
id = canvas.create_window(x, y, window=label, anchor="nw")
[222] Fix | Delete
self.canvas = canvas
[223] Fix | Delete
self.label = label
[224] Fix | Delete
self.id = id
[225] Fix | Delete
label.bind("<ButtonPress>", self.press)
[226] Fix | Delete
[227] Fix | Delete
def detach(self):
[228] Fix | Delete
canvas = self.canvas
[229] Fix | Delete
if not canvas:
[230] Fix | Delete
return
[231] Fix | Delete
id = self.id
[232] Fix | Delete
label = self.label
[233] Fix | Delete
self.canvas = self.label = self.id = None
[234] Fix | Delete
canvas.delete(id)
[235] Fix | Delete
label.destroy()
[236] Fix | Delete
[237] Fix | Delete
def press(self, event):
[238] Fix | Delete
if dnd_start(self, event):
[239] Fix | Delete
# where the pointer is relative to the label widget:
[240] Fix | Delete
self.x_off = event.x
[241] Fix | Delete
self.y_off = event.y
[242] Fix | Delete
# where the widget is relative to the canvas:
[243] Fix | Delete
self.x_orig, self.y_orig = self.canvas.coords(self.id)
[244] Fix | Delete
[245] Fix | Delete
def move(self, event):
[246] Fix | Delete
x, y = self.where(self.canvas, event)
[247] Fix | Delete
self.canvas.coords(self.id, x, y)
[248] Fix | Delete
[249] Fix | Delete
def putback(self):
[250] Fix | Delete
self.canvas.coords(self.id, self.x_orig, self.y_orig)
[251] Fix | Delete
[252] Fix | Delete
def where(self, canvas, event):
[253] Fix | Delete
# where the corner of the canvas is relative to the screen:
[254] Fix | Delete
x_org = canvas.winfo_rootx()
[255] Fix | Delete
y_org = canvas.winfo_rooty()
[256] Fix | Delete
# where the pointer is relative to the canvas widget:
[257] Fix | Delete
x = event.x_root - x_org
[258] Fix | Delete
y = event.y_root - y_org
[259] Fix | Delete
# compensate for initial pointer offset
[260] Fix | Delete
return x - self.x_off, y - self.y_off
[261] Fix | Delete
[262] Fix | Delete
def dnd_end(self, target, event):
[263] Fix | Delete
pass
[264] Fix | Delete
[265] Fix | Delete
[266] Fix | Delete
class Tester:
[267] Fix | Delete
[268] Fix | Delete
def __init__(self, root):
[269] Fix | Delete
self.top = tkinter.Toplevel(root)
[270] Fix | Delete
self.canvas = tkinter.Canvas(self.top, width=100, height=100)
[271] Fix | Delete
self.canvas.pack(fill="both", expand=1)
[272] Fix | Delete
self.canvas.dnd_accept = self.dnd_accept
[273] Fix | Delete
[274] Fix | Delete
def dnd_accept(self, source, event):
[275] Fix | Delete
return self
[276] Fix | Delete
[277] Fix | Delete
def dnd_enter(self, source, event):
[278] Fix | Delete
self.canvas.focus_set() # Show highlight border
[279] Fix | Delete
x, y = source.where(self.canvas, event)
[280] Fix | Delete
x1, y1, x2, y2 = source.canvas.bbox(source.id)
[281] Fix | Delete
dx, dy = x2-x1, y2-y1
[282] Fix | Delete
self.dndid = self.canvas.create_rectangle(x, y, x+dx, y+dy)
[283] Fix | Delete
self.dnd_motion(source, event)
[284] Fix | Delete
[285] Fix | Delete
def dnd_motion(self, source, event):
[286] Fix | Delete
x, y = source.where(self.canvas, event)
[287] Fix | Delete
x1, y1, x2, y2 = self.canvas.bbox(self.dndid)
[288] Fix | Delete
self.canvas.move(self.dndid, x-x1, y-y1)
[289] Fix | Delete
[290] Fix | Delete
def dnd_leave(self, source, event):
[291] Fix | Delete
self.top.focus_set() # Hide highlight border
[292] Fix | Delete
self.canvas.delete(self.dndid)
[293] Fix | Delete
self.dndid = None
[294] Fix | Delete
[295] Fix | Delete
def dnd_commit(self, source, event):
[296] Fix | Delete
self.dnd_leave(source, event)
[297] Fix | Delete
x, y = source.where(self.canvas, event)
[298] Fix | Delete
source.attach(self.canvas, x, y)
[299] Fix | Delete
[300] Fix | Delete
[301] Fix | Delete
def test():
[302] Fix | Delete
root = tkinter.Tk()
[303] Fix | Delete
root.geometry("+1+1")
[304] Fix | Delete
tkinter.Button(command=root.quit, text="Quit").pack()
[305] Fix | Delete
t1 = Tester(root)
[306] Fix | Delete
t1.top.geometry("+1+60")
[307] Fix | Delete
t2 = Tester(root)
[308] Fix | Delete
t2.top.geometry("+120+60")
[309] Fix | Delete
t3 = Tester(root)
[310] Fix | Delete
t3.top.geometry("+240+60")
[311] Fix | Delete
i1 = Icon("ICON1")
[312] Fix | Delete
i2 = Icon("ICON2")
[313] Fix | Delete
i3 = Icon("ICON3")
[314] Fix | Delete
i1.attach(t1.canvas)
[315] Fix | Delete
i2.attach(t2.canvas)
[316] Fix | Delete
i3.attach(t3.canvas)
[317] Fix | Delete
root.mainloop()
[318] Fix | Delete
[319] Fix | Delete
[320] Fix | Delete
if __name__ == '__main__':
[321] Fix | Delete
test()
[322] Fix | Delete
[323] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function