Introduction
The "Hello, World" of drawing on windows.
What Objects are Involved?
wx.PaintDC - DeviceContext to draw on, in response to EVT_PAINT.
- wx.Pen - Pen to draw with.
Process Overview
Draw on a wx.PaintDC with a wx.Pen in response to wx.EVT_PAINT.
Special Concerns
Read more RecipesImagesAndGraphics to learn more and extend beyond the limit.
Code Sample
import wx class DrawPanel(wx.Frame): """Draw a line to a panel.""" def __init__(self): wx.Frame.__init__(self, title="Draw on Panel") self.Bind(wx.EVT_PAINT, self.OnPaint) def OnPaint(self, event=None): dc = wx.PaintDC(self) dc.Clear() dc.SetPen(wx.Pen(wx.BLACK, 4)) dc.DrawLine(0, 0, 50, 50) app = wx.App(False) frame = DrawPanel() frame.Show() app.MainLoop()
Another Way
import wx app = wx.App(False) frame = wx.Frame(None, title="Draw on Panel") panel = wx.Panel(frame) def on_paint(event): dc = wx.PaintDC(event.GetEventObject()) dc.Clear() dc.SetPen(wx.Pen("BLACK", 4)) dc.DrawLine(0, 0, 50, 50) panel.Bind(wx.EVT_PAINT, on_paint) frame.Show(True) app.MainLoop()
This doesn't create your own class, but you probably will want to. Notice that the paint function takes only one argument here, not two.
See Also
Comments
By LionKimbro.
Add comments here.