-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTkDND_wrapper.py
103 lines (92 loc) · 4.75 KB
/
TkDND_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#
# This wrapper was copied from https://mail.python.org/pipermail/tkinter-discuss/2005-July/000476.html
# parse_uri_list(self, uri_list) method was added by the author
#
import os
import tkinter
class TkDND(object):
def __init__(self, tkroot):
self._tkroot = tkroot
# make self an attribute of the parent window for easy access in child classes
tkroot.dnd = self
def bindsource(self, widget, type=None, command=None, arguments=None, priority=None):
'''Register widget as drag source; for details on type, command and arguments, see bindtarget().
priority can be a value between 1 and 100, where 100 is the highest available priority (default: 50).
If command is omitted, return the current binding for type; if both type and command are omitted,
return a list of registered types for widget.'''
command = self._generate_callback(command, arguments)
tkcmd = self._generate_tkcommand('bindsource', widget, type, command, priority)
res = self._tkroot.tk.eval(tkcmd)
if type == None:
res = res.split()
return res
def bindtarget(self, widget, type=None, sequence=None, command=None, arguments=None, priority=None):
'''Register widget as drop target; type may be one of text/plain, text/uri-list, text/plain;charset=UTF-8
(see the man page tkDND for details on other (platform specific) types);
sequence may be one of '<Drag>', '<DragEnter>', '<DragLeave>', '<Drop>' or '<Ask>' ;
command is the callback associated with the specified event, argument is an optional tuple of arguments
that will be passed to the callback; possible arguments include: %A %a %b %C %c %D %d %L %m %T %t %W %X %x %Y %y
(see the tkDND man page for details); priority may be a value in the range 1 to 100 ; if there are
bindings for different types, the one with the priority value will be proceeded first (default: 50).
If command is omitted, return the current binding for type, where sequence defaults to '<Drop>'.
If both type and command are omitted, return a list of registered types for widget.'''
command = self._generate_callback(command, arguments)
tkcmd = self._generate_tkcommand('bindtarget', widget, type, sequence, command, priority)
res = self._tkroot.tk.eval(tkcmd)
if type == None:
res = res.split()
return res
def clearsource(self, widget):
'''Unregister widget as drag source.'''
self._tkroot.tk.call('dnd', 'clearsource', widget)
def cleartarget(self, widget):
'''Unregister widget as drop target.'''
self._tkroot.tk.call('dnd', 'cleartarget', widget)
def drag(self, widget, actions=None, descriptions=None, cursorwindow=None, command=None, arguments=None):
'''Initiate a drag operation with source widget.'''
command = self._generate_callback(command, arguments)
if actions:
if actions[1:]:
actions = '-actions {%s}' % ' '.join(actions)
else:
actions = '-actions %s' % actions[0]
if descriptions:
descriptions = ['{%s}'%i for i in descriptions]
descriptions = '{%s}' % ' '.join(descriptions)
if cursorwindow:
cursorwindow = '-cursorwindow %s' % cursorwindow
tkcmd = self._generate_tkcommand('drag', widget, actions, descriptions, cursorwindow, command)
self._tkroot.tk.eval(tkcmd)
def _generate_callback(self, command, arguments):
'''Register command as tk callback with an optional list of arguments.'''
cmd = None
if command:
cmd = self._tkroot._register(command)
if arguments:
cmd = '{%s %s}' % (cmd, ' '.join(arguments))
return cmd
def _generate_tkcommand(self, base, widget, *opts):
'''Create the command string that will be passed to tk.'''
tkcmd = 'dnd %s %s' % (base, widget)
for i in opts:
if i is not None:
tkcmd += ' %s' % i
return tkcmd
def parse_uri_list(self, uri_list):
space_in_path = False
unprocessed_file_list = uri_list.split(' ')
file_list = []
processed_path = ''
for item in unprocessed_file_list:
if('{' in item):
space_in_path = True
processed_path = item.replace('{', '')
elif('}' in item):
space_in_path = False
processed_path += ' ' + item.replace('}', '')
file_list.append(processed_path)
elif(space_in_path):
processed_path+= ' ' + item
else:
file_list.append(item)
return file_list