2015-03-03 65 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()