fig.canvas.mpl_connect(‘button_release_event’,self.btn_release)
等等,我找不到任何会返回放在它上面的文件路径的东西.
我还没有达到我想用嵌入式图创建GUI的水平,所以如果有一个没有这样做的解决方案,我宁愿这样做.
解决方法 你似乎希望drop_file_event由mpl_connect处理;不幸的是,根据 documentation的情况并非如此(无论如何,这是一个通用绘图库的具体情况).但是,实现自己的GUI处理drop事件并不困难.下面是一个主要基于@L_502_1@并添加wx.fileDropTarget的示例.
# Used to guarantee to use at least Wx2.8import wxversionwxversion.ensureMinimal('2.8')import numpy as npimport wximport matplotlibmatplotlib.use('WXAgg')from matplotlib.backends.backend_wxagg import figureCanvasWxAgg as figureCanvasfrom matplotlib.backends.backend_wx import NavigationToolbar2Wxfrom matplotlib.figure import figureclass 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),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,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()总结
以上是内存溢出为你收集整理的python – Matplotlib拖放文件全部内容,希望文章能够帮你解决python – Matplotlib拖放文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)