2014-06-16 10 views
3

내 Matplotlib 플롯에 파일을 끌어다 놓을 수 있으며 파일을 열고 (예 : 열려 있고 플롯해야 함) 할 수 있습니다. 불행히도, 내 스크립트가 사용하는 몇 가지 이벤트를 처리하는 동안 : fig.canvas.mpl_connect('button_release_event', self.btn_release) 등, 나는 그것에 떨어 뜨린 파일의 경로를 반환합니다 아무것도 찾을 수 없습니다.Matplotlib 끌어서 놓기 파일

나는 임베디드 플롯이있는 GUI를 만들려는 수준이 아니므로 그렇게하지 않고도 해결책이 있다면 선호 할 것입니다.

답변

1

drop_file_event을 원하시는 분 mpl_connect에 의해 처리되었습니다. 불행히도 이것은 documentation에 따른 사례는 아닙니다 (어쨌든 일반 플로팅 라이브러리의 경우 약간의 경우입니다).

그러나 GUI 처리 드롭 이벤트를 구현하는 것은 그렇게 어렵지 않습니다. 다음은 대부분 embedding_in_wx2을 기반으로하고 wx.FileDropTarget을 추가 한 예입니다.

# Used to guarantee to use at least Wx2.8 
import wxversion 
wxversion.ensureMinimal('2.8') 

import numpy as np 
import wx 

import matplotlib 
matplotlib.use('WXAgg') 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 
from matplotlib.backends.backend_wx import NavigationToolbar2Wx 
from matplotlib.figure import Figure 


class MyFileDropTarget(wx.FileDropTarget): 
    def __init__(self, window): 
     wx.FileDropTarget.__init__(self) 
     self.window = window 

    def OnDropFiles(self, x, y, filenames): 
     fig = self.window.figure 
     inaxes = fig.get_axes()[0] 
     h_pix = int(fig.get_figheight() * fig.get_dpi()) # fig height in pixels 
     message = "%d file(s) dropped at (%d,%d):\n" % (len(filenames), x, y) 
     for file in filenames: 
      message += file + "\n" 
     inaxes.annotate(message, (x, h_pix-y), xycoords='figure pixels')   
     self.window.draw() 

class CanvasPanel(wx.Panel): 
    def __init__(self, parent): 
     wx.Panel.__init__(self, parent) 
     self.figure = Figure() 
     self.axes = self.figure.add_subplot(111) 
     self.canvas = FigureCanvas(self, -1, self.figure) 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) 
     self.SetSizer(self.sizer) 
     self.add_toolbar() 
     self.Fit() 

     win_target = self.canvas 
     dt = MyFileDropTarget(win_target) 
     win_target.SetDropTarget(dt) 

    def draw(self): 
     t = np.linspace(0.0, 2., 100) 
     s = np.sin(2 * np.pi * t) 
     self.axes.plot(t, s) 

    def add_toolbar(self): 
     self.toolbar = NavigationToolbar2Wx(self.canvas) 
     self.toolbar.Realize() 
     if wx.Platform == '__WXMAC__': 
      # Mac platform (OSX 10.3, MacPython) does not seem to cope with 
      # having a toolbar in a sizer. This work-around gets the buttons 
      # back, but at the expense of having the toolbar at the top 
      self.SetToolBar(self.toolbar) 
     else: 
      # On Windows platform, default window size is incorrect, so set 
      # toolbar width to figure width. 
      tw, th = self.toolbar.GetSizeTuple() 
      fw, fh = self.canvas.GetSizeTuple() 
      # By adding toolbar in sizer, we are able to put it at the bottom 
      # of the frame - so appearance is closer to GTK version. 
      # As noted above, doesn't work for Mac. 
      self.toolbar.SetSize(wx.Size(fw, th)) 
      self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND) 
     # update the axes menu on the toolbar 
     self.toolbar.update() 

if __name__ == "__main__": 
    app = wx.PySimpleApp() 
    frame = wx.Frame(None, title='File drop test') 
    panel = CanvasPanel(frame) 
    panel.draw() 
    frame.Show() 
    app.MainLoop() 
+0

나는 뭔가를 놓치지 않았 음을 알아두면 좋을 것 같습니다. GUI에 대한 나의 모험은 내가 계획했던 것보다 일찍 시작된다. 예를 들어 주셔서 감사합니다. – user3744040