Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ウィンドウにドラッグアンドドロップでファイルパスを取得できる
import wx
import os
class ComicInfoGetter(wx.App):
def OnInit(self):
frm = GuiWindow("ComicInfo Getter from D&M Lexicon")
frm.Show()
return 1
class GuiWindow(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, -1, title, size=(480, 600), pos=(400, 448))
panel = wx.Panel(self, wx.ID_ANY)
self.url_input = wx.TextCtrl(panel, wx.ID_ANY, 'Info URL')
self.filepath = wx.TextCtrl(panel, wx.ID_ANY, 'Archive Path')
self.openfilebutton = wx.Button(panel, wx.ID_ANY, '...')
self.openfilebutton.Bind(wx.EVT_BUTTON, self.open)
dt = Droptarget(self)
self.SetDropTarget(dt)
line1 = wx.BoxSizer(wx.HORIZONTAL)
line2 = wx.BoxSizer(wx.HORIZONTAL)
layout = wx.BoxSizer(wx.VERTICAL)
line1.Add(self.filepath, proportion=1, flag=wx.GROW)
line1.Add(self.openfilebutton, proportion=0, flag=wx.GROW)
line2.Add(self.url_input, proportion=1, flag=wx.GROW)
layout.Add(line1, proportion=0, flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, border=5)
layout.Add(line2, proportion=0, flag=wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, border=5)
panel.SetSizer(layout)
def open(self, e):
path = ''
dialog = wx.FileDialog(self, "Choose a File", path, "*.*")
if dialog.ShowModal() == wx.ID_OK:
path = os.path.join(dialog.GetDirectory(), dialog.GetFilename())
self.filepath.SetValue(path)
dialog.Destroy()
class Droptarget(wx.FileDropTarget):
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, files):
if len(files) == 1:
self.window.filepath.SetValue(files[0])
else:
wx.MessageBox("I can't do with multiple files!", 'Error')
if __name__ == '__main__':
app = ComicInfoGetter()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment