2015-01-09 26 views
1

我正試圖在wxPython應用程序的面板內並排放置兩個matplotlib數字。這用於使用wxPython 2.8,但不再使用wxPython 3.0。wxPython面板中matplotlib數字的大小不正確

在wxPython 2.8中,每個圖的寬度是面板大小的一半。在wxPython 3.0中,圖的寬度等於面板大小,這意味着只有左側的圖顯示。然後展開窗戶,最終顯示右側的情節。

最簡單的例子,再現問題如下。

import wx 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg 
from matplotlib.figure import Figure 

class MainFrame(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     wx.Frame.__init__(self, *args, **kwargs) 

     panel = wx.Panel(self) 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     sizer.Add(TestPlot(panel), 1, wx.EXPAND | wx.ALL, border=5) 
     sizer.Add(TestPlot(panel), 1, wx.EXPAND | wx.ALL, border=5) 
     panel.SetSizerAndFit(sizer) 

class TestPlot(wx.Window): 
    def __init__(self, *args, **kwargs): 
     wx.Window.__init__(self, *args, **kwargs) 

     self.canvas = FigureCanvasWxAgg(self, wx.ID_ANY, Figure()) 

     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     sizer.Add(self.canvas, 1, wx.ALL, border=20) 
     self.SetSizerAndFit(sizer) 

if __name__ == "__main__": 
    app = wx.App() 
    MainFrame(parent=None, size=(300, 300)).Show() 
    app.MainLoop() 

如何讓這個佈局在wxPython 3.0中工作?

+0

我不知道WX框架可言,但是從你的描述和代碼味道我猜測的問題是wx改變了它處理sizer.Add(TestPlot(panel),1,wx.EXPAND | wx.ALL,border = 5)'' – tacaswell 2015-01-11 00:46:29

回答

0

替換

panel.SetSizerAndFit(sizer) 

通過

panel.SetSizer(sizer) 
    sizer.Fit(self) 

以最終調整wx.Framepanel的元素。

0

我在wxpython 3.0上遇到了同樣的問題。

我認爲問題在於self.canvas的默認最小尺寸太大。

通過self.canvas.SetMinSize(wx.Size(1,1)) 似乎可以手動將最小尺寸設置爲一些非常小的值來解決問題。雖然不是一種優雅的方式...

以下是您的示例的固定版本。

import wx 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg 
from matplotlib.figure import Figure 

class MainFrame(wx.Frame): 
    def __init__(self, *args, **kwargs): 
     wx.Frame.__init__(self, *args, **kwargs) 

     panel = wx.Panel(self) 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     sizer.Add(TestPlot(panel), 1, wx.EXPAND | wx.ALL, border=5) 
     sizer.Add(TestPlot(panel), 1, wx.EXPAND | wx.ALL, border=5) 
     panel.SetSizer(sizer) 
     # don't do Fit(), as it sets the canvases to its minimum size 
     panel.Layout() 

class TestPlot(wx.Window): 
    def __init__(self, *args, **kwargs): 
     wx.Window.__init__(self, *args, **kwargs) 

     self.canvas = FigureCanvasWxAgg(self, wx.ID_ANY, Figure()) 
     # setting the minimum canvas size as small as possible 
     self.canvas.SetMinSize(wx.Size(1,1)) 

     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     # added wx.EXPAND so that the canvas can stretch vertically 
     sizer.Add(self.canvas, 1, wx.ALL|wx.EXPAND, border=20) 
     self.SetSizer(sizer) 

if __name__ == "__main__": 
    app = wx.App() 
    MainFrame(parent=None, size=(300, 300)).Show() 
    app.MainLoop() 

希望這會有所幫助。

測試在Windows 8.1/Python的2.7.9/1.4.3 matplotlib/wxPython的3.0.0.0