import tkinter as Tk
# from tkconstants import *
import ctypes
import sys
from ctypes.wintypes import HWND, UINT, WPARAM, LPARAM
prototype = ctypes.WINFUNCTYPE(ctypes.c_long, HWND,
UINT, WPARAM, LPARAM)
WM_DROPFILES = 0x0233
GWL_WNDPROC = -4
WINPROC = None
Gnames = "初期値"
def py_drop_func(hwnd, msg, wp, lp):
u"""D&D用のコールバック
ファイルのドラッグアンドドロップイベント(WM_DROPFILES)を検出して、
ドロップされたファイル名を保持する。
ここでウィンドウ(tk)を使用するとハングアップするのでデータ保存だけ行う。
"""
if msg == WM_DROPFILES:
i = ctypes.windll.shell32.DragQueryFile(wp, -1, None, None)
print( i )
for tmp in range(i):
szFile = ctypes.c_buffer(260)
ctypes.windll.shell32.DragQueryFile(wp, \
tmp, szFile, ctypes.sizeof(szFile))
Gnames = szFile.value.decode( sys.getfilesystemencoding())
wdgButton.append( Gnames )
ctypes.windll.shell32.DragFinish(wp)
return ctypes.windll.user32.CallWindowProcW(\
WINPROC, hwnd, msg, wp, lp)
class WidgetButton():
def __init__( self, win_path ):
self.widget = Tk.Button( win_path, text="Push" )
self.widget.bind("<Button-1>", self.pushed )
self.widget.pack( side='top' )
def append( self, name ):
self.name = name
print( "append: " + name )
#----------------------
# ボタンが押されたら
#----------------------
def pushed( self, event ):
print("押された")
print( self.name )
class TkApp( Tk.Frame):
dropnames = []
def __init__(self, *args, **kargs):
Tk.Frame.__init__(self, *args, **kargs)
self.createwidget()
def drop_check():
if TkApp.dropnames:
tmp = TkApp.dropnames
TkApp.dropnames = []
self.open(tmp)
self._root().after(10,drop_check)
self._root().after(10,drop_check)
def createwidget(self):
u"何かウィジット作成"
pass
def open(self, filenames):
u"ファイルを開く"
pass
if __name__ == "__main__":
win_root = Tk.Tk()
a = TkApp()
wdgButton = WidgetButton( win_root )
hwnd = a._root().winfo_id()
ctypes.windll.shell32.DragAcceptFiles(hwnd, True)
WINPROC = ctypes.windll.user32.GetWindowLongW(\
hwnd, GWL_WNDPROC)
drop_func = prototype(py_drop_func)
ctypes.windll.user32.SetWindowLongW(hwnd, \
GWL_WNDPROC, drop_func)
a.mainloop()