在WXPYTHON面板中放置matplotlib图时遇到的问题

时间:2019-06-14 12:07:51

标签: python matplotlib wxpython

我试图将matplotlib图形和NavigationToolbar小部件嵌入wx.panel中,但是图形和导航Toolbar的位置在面板内部未正确发生。

我也尝试过使用wxmplot库和sizer,但仍未实现我要搜索的内容。

下面是我正在使用的代码:

import wx
import wx.xrc
import wx.adv
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar


class MainFrame ( wx.Frame ):
    def __init__( self, parent ):
        #main frame
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 728,467 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.Maximize( True)
        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        self.SetBackgroundColour( wx.Colour( 208, 208, 208 ) )
        #Creating First sizer
        mainSizer = wx.BoxSizer( wx.VERTICAL )

        #Creating first sizer on the vertical sizer mainSizer
        firstSizer = wx.FlexGridSizer( 0, 2, 0, 0 )
        firstSizer.SetFlexibleDirection( wx.BOTH )
        firstSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )

        #creating tabs in the first sizer
        self.Tabs = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.Size( 400,500 ), 0 )

        #creating page(report generator page) for the first tab
        self.Report_Generator = wx.Panel( self.Tabs, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        reportGeneratorSizer = wx.FlexGridSizer( 0, 3, 0, 0 )
        reportGeneratorSizer.SetFlexibleDirection( wx.BOTH )
        reportGeneratorSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        self.Report_Generator.SetSizer( reportGeneratorSizer )
        self.Report_Generator.Layout()
        reportGeneratorSizer.Fit( self.Report_Generator )
        self.Tabs.AddPage( self.Report_Generator, u"Report Generator", False )

        # creating page(Graph generator page) for the second tab
        self.Graph_Generator = wx.Panel( self.Tabs, wx.ID_ANY, wx.DefaultPosition, wx.Size( 50,-1 ), wx.TAB_TRAVERSAL )
        GraphGeneratorSizer = wx.FlexGridSizer( 0, 3, 0, 0 )
        GraphGeneratorSizer.SetFlexibleDirection( wx.BOTH )
        GraphGeneratorSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
        self.Graph_Generator.SetSizer( GraphGeneratorSizer )
        self.Graph_Generator.Layout()
        self.Tabs.AddPage( self.Graph_Generator, u"Graph Generator", True )

        #adding tab to the sizer
        firstSizer.Add( self.Tabs, 1, wx.EXPAND |wx.ALL, 5 )

        # creating tabs for holding output
        self.OutPutTabs = wx.Notebook(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(1000, 700), 0)
        # creating page for the output tab
        self.Graph_section = wx.Panel( self.OutPutTabs, wx.ID_ANY, wx.DefaultPosition, wx.Size( 50,-1), wx.TAB_TRAVERSAL )
        self.Graph_section.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))

        DrawGraphHorSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.figure = mpl.figure.Figure(dpi = 100, figsize=(460*1.0/100, 700*1.0/100))
        self.figure.add_subplot(111).plot([1, 2, 3], [2, 1, 4])
        self.canvas = FigureCanvas(self,-1,self.figure)
        self.toolbar = NavigationToolbar(self.canvas)
        self.toolbar.Realize()
        DrawGraphHorSizer.Add( ( 0, 0), 1, wx.EXPAND, 5 )
        DrawGraphHorSizer.Add( ( 0, 0), 1, wx.EXPAND, 5 )
        DrawGraphHorSizer.Add(self.canvas,1,wx.ALIGN_RIGHT|wx.EXPAND)
        DrawGraphHorSizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
        self.Graph_section.SetSizer(DrawGraphHorSizer)
        DrawGraphHorSizer.Fit(self)
        self.OutPutTabs.AddPage(self.Graph_section, u"Graph", True)

        firstSizer.Add( self.OutPutTabs, 1, wx.EXPAND |wx.ALL, 0 )
        mainSizer.Add( firstSizer, 1, wx.ALL, 0 )
        self.SetSizer( mainSizer )


app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

我不确定您会发生什么。

似乎您可能打算将FigureCanvas不在主面板(self)上,而是在Graph_section上。如果是这样,则进行更改

self.canvas = FigureCanvas(self,-1,self.figure)

self.canvas = FigureCanvas(self.Graph_section, -1, self.figure)

可能会做你想做的事。