wxpython全新,所以我的问题就在这个帖子上(Embedding a matplotlib figure inside a WxPython panel)
如何为用户添加输入框以输入值或字符串?在xrange值中说6.0而不是3.0,他们再次点击计算?或者他们输入一个导入文件的字符串?
第二个问题,我如何为这些情节制作2个标签?标签1有一个图,标签2有另一个图?
感谢您的帮助
答案 0 :(得分:0)
所以我得到了标签部分来处理这段代码,但现在不确定如何让用户能够输入图表的数据。
import wx
import wx.aui
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as mplCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2Wx as mplToolbar
# initialization code: create a basic Wx application
app = wx.PySimpleApp()
framelist = [] # list of frames (windows) created here
def MakePlotWindow(DisableDelete=False, size=(600,600), **kw):
''' Create a plot window with an empty notebook widget for plots includes a sizer at top
for placement of wx widgets
Input:
size: the dimensions of the plot window in pixels (defaults to 600x600)
other keyword args for wx.Frame are allowed
Output:
returns the name of the created panel (<panel>)
Use <panel>.Parent to access the frame
Note that <panel>.topsizer is a sizer above the plot where wx objects can be placed
and <panel>.bottomsizer is a sizer below the plot where wx objects can be placed
'''
kw["size"] = size
plotter = PlotNotebook(id, DisableDelete, **kw)
global framelist
framelist.append(plotter.frame)
return plotter
def ShowPlots():
'''Call ShowPlots after all plots are defined to cause the plots to be displayed
and to enter the event loop so that widgets respond to mouse clicks, etc.
'''
for frame in framelist:
frame.Show()
app.MainLoop()
class PlotNotebook(wx.Panel):
''' defines a panel in a independent frame (window). Call from MakePlotWindow'''
def __init__(self, id, DisableDelete=False, **kw):
self.frame = wx.Frame(None, **kw) # create a frame
wx.Panel.__init__(self, self.frame) # create the panel
# note the above places this panel inside the new frame
# create a notebook widget:
if DisableDelete:
self.nb = wx.aui.AuiNotebook(
self,
style=wx.aui.AUI_NB_DEFAULT_STYLE ^ wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
# turns off the delete tab
)
else:
self.nb = wx.aui.AuiNotebook(self)
sizer = wx.BoxSizer(wx.VERTICAL) # create a sizer for the notebook
self.topsizer = wx.BoxSizer(wx.HORIZONTAL) # a place for misc wx items
sizer.Add(self.topsizer, 0, wx.EXPAND) # place the widget box in the sizer
sizer.Add(self.nb, 1, wx.EXPAND) # place the notebook in the sizer
self.bottomsizer = wx.BoxSizer(wx.HORIZONTAL) # a place for misc wx items
sizer.Add(self.bottomsizer, 0, wx.EXPAND) # place the widget box in the sizer
self.SetSizer(sizer) # associate the sizer with the panel created by class
def AddPlotTab(self,name="plot"):
'''Adds a plot tab to the panel
Input:
name: a string that is placed on the plot tab
Output:
returns the wx.Panel object that contains the the new plot. Note that
<panel>.figure is the plot figure
<panel>.canvas is the plot canvas
<panel>.toolbar is the toolbar object
<panel>.topsizer is a sizer above the plot where wx objects can be placed
'''
page = Plot(self.nb)
self.nb.AddPage(page,name)
return page
def ReusePlotTab(self,name="plot"):
'''Reuses a plot tab on the panel, if a tab with the selected name exists. Uses the
first one if more than one such tab exists. Creates a tab with the name, if one is
not found.
Input:
name: a string that is placed on the plot tab
Output:
returns the wx.Panel object that contains the the new plot. Note that
<panel>.figure is the plot figure
<panel>.canvas is the plot canvas
<panel>.toolbar is the toolbar object
<panel>.topsizer is a sizer above the plot where wx objects can be placed
'''
for PageNum in range(self.nb.GetPageCount()):
if name == self.nb.GetPageText(PageNum):
Page = self.nb.GetPage(PageNum)
return Page
return self.AddPlotTab(name) # not found, create a new tab
def RaisePlotTab(self,name="plot"):
'''Raises a plot tab on the panel so that the plot is visible. Uses the
first one if more than one such tab exists. Does nothing if no such tab is found'''
for PageNum in range(self.nb.GetPageCount()):
if name == self.nb.GetPageText(PageNum):
self.nb.SetSelection(PageNum)
return
class Plot(wx.Panel):
''' Defines a panel containing a plot etc to be placed in a notebook widget
Note that this is expected to be called only from PlotNotebook.AddPlotTab
'''
def __init__(self, parent, id = -1, dpi = None, **kwargs):
wx.Panel.__init__(self, parent, id=id, **kwargs)
# create a figure with an associated canvas & toolbar
self.figure = mpl.figure.Figure(dpi=dpi)
self.canvas = mplCanvas(self, -1, self.figure) # wxPanel object containing plot
self.toolbar = mplToolbar(self.canvas) # toolbar object
self.toolbar.Realize()
self.topsizer = wx.BoxSizer(wx.HORIZONTAL) # a place for misc wx items
# put the canvas, toolbar & topsizer in a sizer and associate the sizer
# with self (the main panel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.topsizer,0,wx.EXPAND)
sizer.Add(self.canvas,1,wx.EXPAND)
sizer.Add(self.toolbar, 0 , wx.LEFT | wx.EXPAND)
self.SetSizer(sizer)
if __name__ == "__main__":
from numpy import arange, sin, pi
# create the first plot window
plotter = MakePlotWindow(size=(700,700), DisableDelete=True,)
plotter.Parent.SetTitle("Title for 1st Plot Window")
# add a plot to the window with a straight line
page1 = plotter.AddPlotTab('Plot1')
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
page1.figure.gca().plot(t,s,color='red')
# code to respond to a key press inside the window
#page1.figure.gca().set_title("Press any key inside plot") # plot title
#page1.figure.canvas.mpl_connect('key_press_event', onKeyPress)
# create a second plot on the 1st window and raise it
page2 = plotter.AddPlotTab('Plot2')
page2.figure.gca().plot(range(10),[i**2 for i in range(10)],'+-')
plotter.RaisePlotTab('Plot2')
# everything is done, display the plots and start the event loop
ShowPlots()