2015-03-03 64 views
0

我無法發佈擦除後臺事件以繪製到屏幕。在我的完整代碼中,我想在單擊按鈕時繪製位圖(DC.DrawBitmap())。我通過發佈由自定義綁定方法捕獲的EVT_ERASE_BACKGROUND事件來完成此操作。但是,一旦它在該方法中,通常工作的event.GetDC()方法將失敗。wxpython使用DC後擦除背景

這裏是具有相同結果的簡化代碼:

 
import wx 

class Foo(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__ (self, parent, -1, title, size=(500,300)) 
     self.panel = wx.Panel(self, -1) 

     self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 
     self.Bind(wx.EVT_ENTER_WINDOW, self.onEnter) 

     self.Show() 

    def OnEraseBackground(self, e): 
     DC = e.GetDC() 

    def onEnter(self, e): 
     wx.PostEvent(self, wx.PyCommandEvent(wx.wxEVT_ERASE_BACKGROUND)) 

app = wx.App() 
Foo(None, 'foo') 
app.MainLoop() 

這就提出:

AttributeError: 'PyCommandEvent' object has no attribute 'GetDC' 

我該如何解決這個問題?

回答

0

發佈之前,它的工作了一個小時都沒有成功,那麼解決它自己五分鐘後......

這裏是我的解決方案,創造了ClientDC如果事件沒有自己的DC:

 
import wx 

class Foo(wx.Frame): 
    def __init__(self, parent, title): 
     wx.Frame.__init__ (self, parent, -1, title, size=(500,300)) 
     self.panel = wx.Panel(self, -1) 

     self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) 
     self.Bind(wx.EVT_ENTER_WINDOW, self.onEnter) 

     self.Show() 

    def OnEraseBackground(self, e): 
     try: 
      DC = e.GetDC() 
     except: 
      DC = wx.ClientDC(self) 
     DC.Clear() 

    def onEnter(self, e): 
     wx.PostEvent(self, wx.PyCommandEvent(wx.wxEVT_ERASE_BACKGROUND)) 

app = wx.App() 
Foo(None, 'foo') 
app.MainLoop()