[wxPython] Drawing lines on a wx.Frame

Post here if you need help with creating a Graphical User Interface in Python.

[wxPython] Drawing lines on a wx.Frame

Postby hrs » Sat Feb 16, 2013 2:02 pm

I want to draw some lines on a wx.Frame. At some point there may be a few hundred lines that are calculated in some function.
Now I'm unpacking tupples because otherwise all of it goes into dc.DrawLine.x1 and an error occurs that dc.DrawLine.y1 is missing.
Is there a better way to do this? Using python 2.7.3 on linux.

Code: Select all
import wx

class DrawPanel(wx.Frame):

    """Draw a line to a panel."""

    def __init__(self, lines, parent=None):
        wx.Frame.__init__(self, parent, title="Draw on Panel")
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.lines = lines

    def OnPaint(self, event=None):
        dc = wx.PaintDC(self)
        dc.Clear()
        dc.SetPen(wx.Pen(wx.BLACK, 4))
        for line in self.lines:
            dc.DrawLine(*line)

def a_func_that_returns_some_lines():
    return [(0, 0, 50, 50), (0, 50, 50, 0)]


app = wx.App(False)
frame = DrawPanel(a_func_that_returns_some_lines())
frame.Show()
app.MainLoop()
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: Drawing lines on a wx.Frame

Postby stranac » Sat Feb 16, 2013 3:00 pm

Actually, yes, there is a better way.
Use the DrawLineList() method.

So, instead of:
Code: Select all
for line in self.lines:
    dc.DrawLine(*line)

you can use:
Code: Select all
dc.DrawLineList(self.lines)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1137
Joined: Thu Feb 07, 2013 3:42 pm

Re: Drawing lines on a wx.Frame

Postby hrs » Sat Feb 16, 2013 3:15 pm

Great thanks! That looks much better.

And I'll need to skim the class reference better next time because it's right underneath DrawLine() :oops:
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests