2013-04-18 45 views
3

面板(或筆記本)適當調整我已經寫在wxPython的程序工作在窗戶得很好,但在LUNIX測試時我有所有出現在一些Linux的顯示問題。FigureCanvasWxAgg無法在Linux中

這是一個測試應用程序,它展示了在Panel中調整圖像大小的問題,如圖所示,它自身遵循resizingevent,但圖形CanvasWxAgg沒有遵循,但這在Windows中不是問題。

import wx 
import matplotlib.figure as plt 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 
import wx.lib.inspection 

class Test(wx.Frame): 

    def __init__(self): 
     super(Test, self).__init__(parent=None, id=-1) 
     self.figure = plt.Figure() 
     self.panel = wx.Panel(self, 1) 
     self.figurepanel = FigureCanvas(self.panel, -1, self.figure) 
     self.axes1 = self.figure.add_subplot(111) 
     frame_box = wx.BoxSizer(wx.VERTICAL) 
     frame_box.AddStretchSpacer(prop=1) 
     frame_box.Add(self.panel, flag=wx.EXPAND, proportion=2) 
     frame_box.AddStretchSpacer(prop=1) 
     main_box = wx.BoxSizer(wx.HORIZONTAL) 
     main_box.AddStretchSpacer(prop=1) 
     main_box.Add(frame_box, flag=wx.EXPAND, proportion=1) 
     main_box.AddStretchSpacer(prop=1) 
     self.SetSizer(main_box) 
     self.Show() 
     self.Layout() 

def main(): 
    app = wx.App() 
    Test() 
    wx.lib.inspection.InspectionTool().Show() 
    app.MainLoop() 

if __name__ == '__main__': 
    main() 

,我將非常感激已經回答了什麼是:

  • 如何解決在Linux
  • reszing FigureCanvasWxAgg的這個問題是否有GUI編程與一般的方法有什麼區別wxPython的Windows和Linux中

回答

-1

我想我解決了問題。由wx.EVT_SIZE女巫造成這個問題似乎是自動在Windows而不是Linux中。因此,要解決在Linux下所有你需要做的就是到了wx.Panel綁定到wx.EVT_SIZE,然後定義一個apropriate事件處理程序,是以調整大小的護理問題。 什麼做的把戲對我來說是:

#code beneath is a part if the __init__ metod 
self.panel = wx.Panel(self, -1) 
self.figurepanel = FigureCanvas(self.panel, -1, self.figure) 
self.panel.Bind(wx.EVT_SIZE, self.on_size) 
.... 
#the eventhandler for the panel. The method resizes the the figurepanel and the figure. 
def on_size(self, event): 
    pix = self.panel.GetClientSize() 
    self.figure.set_size_inches(pix[0]/self.figure.get_dpi(), 
           pix[1]/self.figure.get_dpi()) 
    x,y = self.panel.GetSize() 
    self.figurepanel.SetSize((x-1, y-1)) 
    self.figurepanel.SetSize((x, y)) 
    self.figurepanel.draw() 
    event.Skip() 
1

有與您發佈的代碼的幾個問題:

  1. 你有水平和垂直間隔是根據需要展開,這將導致中心區域保持相同的形狀
  2. self.figurepanel不是任何sizer的一部分,這意味着它將而不是調整大小,即使其容器self.panel確實。

下面的代碼產生的情節充滿窗口與調整窗口大小:

import wx 
import matplotlib.figure as plt 
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 
import wx.lib.inspection 

class Test(wx.Frame): 

    def __init__(self): 
     super(Test, self).__init__(parent=None, id=-1) 
     self.panel = wx.Panel(self, 1) 
     self.panel.SetBackgroundColour('RED') 

     self.figure = plt.Figure() 
     self.axes1 = self.figure.add_subplot(111) 
     self.figurepanel = FigureCanvas(self.panel, -1, self.figure) 

     main_box = wx.BoxSizer(wx.HORIZONTAL) 
     main_box.Add(self.figurepanel, flag=wx.EXPAND, proportion=1) 
     self.panel.SetSizer(main_box) 

     frame_box = wx.BoxSizer(wx.VERTICAL) 
     frame_box.Add(self.panel, flag=wx.EXPAND, proportion=1) 

     self.SetSizer(frame_box) 
     self.Show() 
     self.Layout() 

def main(): 
    app = wx.App() 
    Test() 
    wx.lib.inspection.InspectionTool().Show() 
    app.MainLoop() 

if __name__ == '__main__': 
    main() 

有沒有必要做任何手動調整自己,是沒有問題的,我想,並調整其大小事件傳播。如果將有比你所看到的多很多破損。