import wx import numpy as np import os, sys import matplotlib matplotlib.use('WXAgg') # The recommended way to use wx with mpl is with the WXAgg backend. from matplotlib.figure import Figure from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas, \ NavigationToolbar2WxAgg as NavigationToolbar import pylab import time class GraphPanel(wx.Panel) : def __init__(self, parent) : self.dpi = 100 self.fig = Figure((3.0, 3.0), dpi=self.dpi) self.panel = wx.Panel(self) self.canvas = FigCanvas(self.panel, -1, self.fig) """ self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW) self.panel.SetSizer(self.vbox) self.vbox.Fit(self) """ class MainFrame(wx.Frame) : def __init__(self, parent) : self.loc = [100, 100] self.size = np.array([800, 600]) wx.Frame.__init__(self, parent, -1, "Frame MPL Test", pos=self.loc, size=self.size) # create a frame self.AdjustPresentation(self, 1.1, [250, 250, 230]) self.CreateStatusBar() # A StatusBar in the bottom of the window self.Menus() def CreatePanel(self): """ Creates the main panel with all the controls on it: * mpl canvas * mpl navigation toolbar * Control panel for interaction """ self.panel = wx.Panel(self) # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # #self.dpi = 100 self.panel_size = np.array([5.0,5.0]) # Must be in inches! self.fig = Figure(self.panel_size) #, dpi=self.dpi) self.canvas = FigCanvas(self.panel, -1, self.fig) self.subplot = self.fig.add_subplot(111) self.vbox = wx.BoxSizer(wx.VERTICAL) self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) #self.vbox.Add(self.toolbar, 0, wx.EXPAND) self.vbox.AddSpacer(10) self.panel.SetSizer(self.vbox) self.vbox.Fit(self) def SetTimer(self, interval) : self.redraw_timer = wx.Timer(self) self.Bind(wx.EVT_TIMER, self.PlotData, self.redraw_timer) self.redraw_timer.Start(milliseconds=interval, oneShot=False) def PlotData(self, event): # self.MakeData references a function in the main program. self.MakeData(self) self.subplot.clear() self.subplot.plot(self.data) self.canvas.draw() #self.canvas.show() def AdjustPresentation(self, client, font_factor, bkg_color) : client.SetBackgroundColour(wx.Color(bkg_color[0], bkg_color[1], bkg_color[2])) #self.SetBackgroundColour(wx.Color(253,253,240)) client.ScaleFontSize(self, font_factor) def ScaleFontSize(self, some_label, factor) : # This scales the fonts within the window but not in the title, menu or status bars. the_font = some_label.GetFont() the_font.SetPointSize(the_font.GetPointSize() * factor) some_label.SetFont(the_font) def Menus(self): # Setting up the menu. menuBar = wx.MenuBar() # define a menubar # Exit menu exit_menu_elements = [ ["E&xit"," Terminate the program", self.OnExit]] # item with alt-key, status bar description, and target procedure exit_menu = self.MakeMenu(exit_menu_elements) menuBar.Append(exit_menu, "&Exit") # add the entire specific menu to the menubar # Information menu info_menu_elements = [ ["&New User","Introduction", self.OnHelp]] info_menu_elements.append(["&About", "Information about this program", self.OnAbout]) info_menu = self.MakeMenu(info_menu_elements) menuBar.Append(info_menu, "&Information") # Files menu #files_menu_elements = [ ["&Edit"," Edit a text file", self.OnEdit]] # item with alt-key, status bar description, and target procedure #files_menu = self.MakeMenu(files_menu_elements) #menuBar.Append(files_menu, "&Files") # add the entire specific menu to the menubar # Add the MenuBar to the Frame. self.SetMenuBar(menuBar) def MakeMenu(self, elements) : the_menu = wx.Menu() for i in range(len(elements)) : if i > 0 : the_menu.AppendSeparator() #the_menu.Append(elements[i][0] , elements[i][1], elements[i][2]) # add "exit" option to the menu item = the_menu.Append(wx.ID_ANY, elements[i][0], elements[i][1]) #hook the event - amazingly, the procedure is passed as an argument, that is, elements[i][2] is a procedure! #self.Connect(elements[i][0], -1, wx.wxEVT_COMMAND_MENU_SELECTED, elements[i][3]) self.Bind(wx.EVT_MENU, elements[i][2], item) return the_menu def OnExit(self, event): self.Close(True) # Close the frame. sys.exit(0) def OnAbout(self,event): d= wx.MessageDialog( self, "Matplotlib with WX Frame Test: \n" + "Name:\t frame_mpl_test.py\n" + "Author:\t W. Hetherington\n" + "Created:\t 2009/06/10\n" + "Modified:\t 2009/06/10\n" + "Copyright:\t (c) 2009\n" + "License:\t No restrictions\n", "About Frame MPL Test", wx.ICON_INFORMATION) # Create a message dialog box d.ShowModal() # Shows it d.Destroy() # finally destroy it when finished. def OnHelp(self,event): d= wx.MessageDialog( self, "No information yet. \n\n" +"No information concerning when information will be available.\n\n", "For New Users", wx.ICON_INFORMATION) # Create a message dialog box d.ShowModal() # Shows it d.Destroy() # finally destroy it when finished.