嵌入Wx3

版权所有(C)2003-2004 Andrew Straw和Jeremy O’Donoghue等人

许可证:此作品根据PSF许可。该文档也应该在 https://docs.python.org/3/license.html 上提供

这是使用matplotlib和wx的另一个例子。 希望这是功能齐全的:

  • matplotlib工具栏和WX按钮
  • 完整的wxApp框架,包括小部件交互
  • XRC(XML wxWidgets资源)文件创建GUI(用XRCed制作)

这是从embedding_in_wx和dynamic_image_wxagg派生的。

感谢matplotlib和wx团队创建这样出色的软件!

  1. import matplotlib
  2. import matplotlib.cm as cm
  3. import matplotlib.cbook as cbook
  4. from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
  5. from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
  6. from matplotlib.figure import Figure
  7. import numpy as np
  8. import wx
  9. import wx.xrc as xrc
  10. ERR_TOL = 1e-5 # floating point slop for peak-detection
  11. matplotlib.rc('image', origin='lower')
  12. class PlotPanel(wx.Panel):
  13. def __init__(self, parent):
  14. wx.Panel.__init__(self, parent, -1)
  15. self.fig = Figure((5, 4), 75)
  16. self.canvas = FigureCanvas(self, -1, self.fig)
  17. self.toolbar = NavigationToolbar(self.canvas) # matplotlib toolbar
  18. self.toolbar.Realize()
  19. # self.toolbar.set_active([0,1])
  20. # Now put all into a sizer
  21. sizer = wx.BoxSizer(wx.VERTICAL)
  22. # This way of adding to sizer allows resizing
  23. sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
  24. # Best to allow the toolbar to resize!
  25. sizer.Add(self.toolbar, 0, wx.GROW)
  26. self.SetSizer(sizer)
  27. self.Fit()
  28. def init_plot_data(self):
  29. a = self.fig.add_subplot(111)
  30. x = np.arange(120.0) * 2 * np.pi / 60.0
  31. y = np.arange(100.0) * 2 * np.pi / 50.0
  32. self.x, self.y = np.meshgrid(x, y)
  33. z = np.sin(self.x) + np.cos(self.y)
  34. self.im = a.imshow(z, cmap=cm.RdBu) # , interpolation='nearest')
  35. zmax = np.max(z) - ERR_TOL
  36. ymax_i, xmax_i = np.nonzero(z >= zmax)
  37. if self.im.origin == 'upper':
  38. ymax_i = z.shape[0] - ymax_i
  39. self.lines = a.plot(xmax_i, ymax_i, 'ko')
  40. self.toolbar.update() # Not sure why this is needed - ADS
  41. def GetToolBar(self):
  42. # You will need to override GetToolBar if you are using an
  43. # unmanaged toolbar in your frame
  44. return self.toolbar
  45. def OnWhiz(self, evt):
  46. self.x += np.pi / 15
  47. self.y += np.pi / 20
  48. z = np.sin(self.x) + np.cos(self.y)
  49. self.im.set_array(z)
  50. zmax = np.max(z) - ERR_TOL
  51. ymax_i, xmax_i = np.nonzero(z >= zmax)
  52. if self.im.origin == 'upper':
  53. ymax_i = z.shape[0] - ymax_i
  54. self.lines[0].set_data(xmax_i, ymax_i)
  55. self.canvas.draw()
  56. class MyApp(wx.App):
  57. def OnInit(self):
  58. xrcfile = cbook.get_sample_data('embedding_in_wx3.xrc',
  59. asfileobj=False)
  60. print('loading', xrcfile)
  61. self.res = xrc.XmlResource(xrcfile)
  62. # main frame and panel ---------
  63. self.frame = self.res.LoadFrame(None, "MainFrame")
  64. self.panel = xrc.XRCCTRL(self.frame, "MainPanel")
  65. # matplotlib panel -------------
  66. # container for matplotlib panel (I like to make a container
  67. # panel for our panel so I know where it'll go when in XRCed.)
  68. plot_container = xrc.XRCCTRL(self.frame, "plot_container_panel")
  69. sizer = wx.BoxSizer(wx.VERTICAL)
  70. # matplotlib panel itself
  71. self.plotpanel = PlotPanel(plot_container)
  72. self.plotpanel.init_plot_data()
  73. # wx boilerplate
  74. sizer.Add(self.plotpanel, 1, wx.EXPAND)
  75. plot_container.SetSizer(sizer)
  76. # whiz button ------------------
  77. whiz_button = xrc.XRCCTRL(self.frame, "whiz_button")
  78. whiz_button.Bind(wx.EVT_BUTTON, self.plotpanel.OnWhiz)
  79. # bang button ------------------
  80. bang_button = xrc.XRCCTRL(self.frame, "bang_button")
  81. bang_button.Bind(wx.EVT_BUTTON, self.OnBang)
  82. # final setup ------------------
  83. sizer = self.panel.GetSizer()
  84. self.frame.Show(1)
  85. self.SetTopWindow(self.frame)
  86. return True
  87. def OnBang(self, event):
  88. bang_count = xrc.XRCCTRL(self.frame, "bang_count")
  89. bangs = bang_count.GetValue()
  90. bangs = int(bangs) + 1
  91. bang_count.SetValue(str(bangs))
  92. if __name__ == '__main__':
  93. app = MyApp(0)
  94. app.MainLoop()

下载这个示例