Introduction

The "Hello, World" of drawing on windows.

What Objects are Involved?

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.

VerySimpleDrawing (last edited 2010-01-17 16:26:24 by 79-65-133-135)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.