import wx class View(wx.Panel): def __init__(self, parent): super(View, self).__init__(parent) self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) self.Bind(wx.EVT_SIZE, self.on_size) self.Bind(wx.EVT_PAINT, self.on_paint) def on_size(self, event): # 没有这个事件,改变尺寸的时候就全乱了 event.Skip() self.Refresh() def on_paint(self, event): w, h = self.GetClientSize() dc = wx.AutoBufferedPaintDC(self) # 图像被部分覆盖,移开后可随时恢复图像,但改成PaintDC就不行 dc.Clear() dc.DrawLine(0, 0, w, h) dc.SetPen(wx.Pen(wx.BLACK, 5)) dc.DrawCircle(w / 2, h / 2, 100) class Frame(wx.Frame): def __init__(self): super(Frame, self).__init__(None) self.SetTitle('My Title') self.SetClientSize((500, 500)) self.Center() self.view = View(self) def main(): app = wx.App(False) frame = Frame() frame.Show() app.MainLoop() if __name__ == '__main__': main()