# 2DGrafik.py import wx import colorsys from math import cos, sin, radians BASE = 20.0 # sizes used in shapes drawn below USE_BUFFER = ('wxMSW' in wx.PlatformInfo) # use buffered drawing on Windows class TestPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent, -1) self.Bind(wx.EVT_PAINT, self.OnPaint) if USE_BUFFER: self.Bind(wx.EVT_SIZE, self.OnSize) def OnSize(self, evt): self.InitBuffer() evt.Skip() def OnPaint(self, evt): if USE_BUFFER: # The buffer already contains our drawing, so no need to # do anything else but create the buffered DC. When this # method exits and dc is collected then the buffer will be # blitted to the paint DC automagically dc = wx.BufferedPaintDC(self, self._buffer) else: # Otherwise we need to draw our content to the paint DC at # this time. dc = wx.PaintDC(self) gc = self.MakeGC(dc) self.Draw(gc) def InitBuffer(self): sz = self.GetClientSize() sz.width = max(1, sz.width) sz.height = max(1, sz.height) self._buffer = wx.EmptyBitmap(sz.width, sz.height, 32) dc = wx.MemoryDC(self._buffer) dc.SetBackground(wx.Brush("WHITE")) # Original:self.GetBackgroundColour() dc.Clear() gc = self.MakeGC(dc) self.Draw(gc) def MakeGC(self, dc): try: gc = wx.GraphicsContext.Create(dc) except NotImplementedError: dc.DrawText("This build of wxPython does not support the wx.GraphicsContext " "family of classes.", 25, 25) return None return gc def Draw(self, gc): font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) font.SetWeight(wx.BOLD) gc.SetFont(font, wx.BLACK) # wird ignoriert # make a path path = gc.CreatePath() path.AddCircle(0, 0, BASE) path2 = gc.CreatePath() path2.AddRectangle(-1.5*BASE, -1.5*BASE, 3*BASE, 8*BASE) hellrot="red" rot=wx.Colour(95, 0, 0) hellgelb=wx.Colour(255, 255, 0) gelb=wx.Colour(127, 127, 15) hellgruen=wx.Colour(0, 255, 0) gruen=wx.Colour(0, 95, 0) gc.PushState() # save current translation/scale/other state gc.Translate(50, 60) # reposition the context origin gc.SetPen(wx.Pen("black", 1)) gc.SetBrush(wx.Brush("GREY")) gc.DrawPath(path2) gc.SetBrush(wx.Brush(hellrot)) gc.DrawPath(path) gc.Translate(0, BASE*2.5) gc.SetBrush(wx.Brush(gelb)) gc.DrawPath(path) gc.Translate(0, BASE*2.5) gc.SetBrush(wx.Brush(gruen)) gc.DrawPath(path) # gc.StrokePath(path) # nur Umriss # gc.FillPath(path) # nur gefuellt gc.PopState() gc.PushState() gc.Translate(140, 60) gc.SetPen(wx.Pen("black", 1)) gc.SetBrush(wx.Brush("GREY")) gc.DrawPath(path2) gc.SetBrush(wx.Brush(rot)) gc.DrawPath(path) gc.Translate(0, BASE*2.5) gc.SetBrush(wx.Brush(hellgelb)) gc.DrawPath(path) gc.Translate(0, BASE*2.5) gc.SetBrush(wx.Brush(gruen)) gc.DrawPath(path) gc.PopState() gc.PushState() gc.Translate(230, 60) gc.SetPen(wx.Pen("black", 1)) gc.SetBrush(wx.Brush("GREY")) gc.DrawPath(path2) gc.SetBrush(wx.Brush(rot)) gc.DrawPath(path) gc.Translate(0, BASE*2.5) gc.SetBrush(wx.Brush(gelb)) gc.DrawPath(path) gc.Translate(0, BASE*2.5) gc.SetBrush(wx.Brush(hellgruen)) gc.DrawPath(path) gc.PopState() class Fenster(wx.Frame): """ Die Klasse Fenster erzeugt einen Frame. """ def __init__(self, parent, title): wx.Frame.__init__(self, parent, -1, title, pos=(100, 100), size=(300, 300)) self.panel = TestPanel(self) class MyApp(wx.App): def OnInit(self): frame = Fenster(None, "Grafik") self.SetTopWindow(frame) frame.Show(True) return True app = MyApp(redirect=False) # Parameterwert True, wenn Ausgaben in der Standard E/A angezeigt werden sollen app.MainLoop()