⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 embedding_in_wx4.py

📁 非原创。很好的python例子
💻 PY
字号:
#!/usr/bin/env python"""An example of how to use wx or wxagg in an application with a customtoolbar"""from matplotlib.numerix import arange, sin, piimport matplotlib# uncomment the following to use wx rather than wxagg#matplotlib.use('WX')#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas# comment out the following to use wx rather than wxaggmatplotlib.use('WXAgg')from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvasfrom matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg from matplotlib.backends.backend_wx import _load_bitmapfrom matplotlib.figure import Figurefrom matplotlib.numerix.mlab import randfrom wxPython.wx import *class MyNavigationToolbar(NavigationToolbar2WxAgg):    """    Extend the default wx toolbar with your own event handlers    """    ON_CUSTOM = wxNewId()    def __init__(self, canvas, cankill):        NavigationToolbar2WxAgg.__init__(self, canvas)        # for simplicity I'm going to reuse a bitmap from wx, you'll        # probably want to add your own.        self.AddSimpleTool(self.ON_CUSTOM, _load_bitmap('stock_left.xpm'),                           'Click me', 'Activate custom contol')        EVT_TOOL(self, self.ON_CUSTOM, self._on_custom)    def _on_custom(self, evt):        # add some text to the axes in a random location in axes (0,1)        # coords) with a random color        # get the axes        ax = self.canvas.figure.axes[0]        # generate a random location can color        x,y = tuple(rand(2))        rgb = tuple(rand(3))        # add the text and draw        ax.text(x, y, 'You clicked me',                transform=ax.transAxes,                color=rgb)        self.canvas.draw()        evt.Skip()class CanvasFrame(wxFrame):        def __init__(self):        wxFrame.__init__(self,None,-1,                         'CanvasFrame',size=(550,350))        self.SetBackgroundColour(wxNamedColor("WHITE"))        self.figure = Figure(figsize=(5,4), dpi=100)        self.axes = self.figure.add_subplot(111)        t = arange(0.0,3.0,0.01)        s = sin(2*pi*t)                self.axes.plot(t,s)        self.canvas = FigureCanvas(self, -1, self.figure)        self.sizer = wxBoxSizer(wxVERTICAL)        self.sizer.Add(self.canvas, 1, wxTOP | wxLEFT | wxEXPAND)        # Capture the paint message                EVT_PAINT(self, self.OnPaint)                self.toolbar = MyNavigationToolbar(self.canvas, True)        self.toolbar.Realize()        if wxPlatform == '__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(wxSize(fw, th))            self.sizer.Add(self.toolbar, 0, wxLEFT | wxEXPAND)        # update the axes menu on the toolbar        self.toolbar.update()          self.SetSizer(self.sizer)        self.Fit()    def OnPaint(self, event):        self.canvas.draw()        event.Skip()        class App(wxApp):        def OnInit(self):        'Create the main window and insert the custom frame'        frame = CanvasFrame()        frame.Show(true)        return trueapp = App(0)app.MainLoop()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -