drawing lines

Hello,

I am trying to use wxPython to draw some simple lines but am not
getting the results that I want. In my user interface, when the user
makes certain actions I want a window to pop up which will have certain
multi-coloured bars displaying some information to them.

This is the code I have for that window, with some sample coordinates
put in there. I get the window with a white background but no line. Do
I need to refresh it somehow after the line is drawn?
Thanks,
Gabriel

···

##############

class PopupFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, ‘Status Windowt’, size=(300,250))
self.sketch = SketchWindow(self,-1)

class SketchWindow(wx.Window):
def init(self, parent, ID):
wx.Window.init(self, parent, ID)
self.SetBackgroundColour(“White”)
self.color = “Black”
self.thickness = 1
self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
self.InitBuffer()

def InitBuffer(self):
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    dc = wx.BufferedDC(None, self.buffer)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    self.DrawLines(dc)
    self.reInitBuffer = False

def DrawLines(self, dc):
    coords = [(12,13,12,14), (12,14,12,15), (12,15,12,16), (13,16,13,17), (14,17,14,18)]
    for eachcoord in coords:
        dc.DrawLine(*eachcoord)

As a follow-up on this, I’ve changed the code slighly here, but am
still not able to get lines drawn to the screen. Am I using the wrong
type of dc? I’ve not tried to draw lines with wxPython before, so am
really not sure.

···

###########

class MyFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, ‘Global Context’, size=(250,500))
self.panel = MyPanel(self,-1)

class MyPanel(wx.Panel):

def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id, size=(250,500))
    self.sketch = SketchWindow(self,-1)

class SketchWindow(wx.Window):
def init(self, parent, ID):
wx.Window.init(self, parent, ID, size=(250,500))
self.SetBackgroundColour(“Red”)
self.color = “White”
self.thickness = 1
self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
size = self.GetClientSize()
self.buffer = wx.EmptyBitmap(size.width, size.height)
dc = wx.BufferedDC(None, self.buffer)
self.PrepareDC(dc)
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
dc.BeginDrawing()
coords = [(12,13,12,14), (12,14,12,15), (12,15,12,16), (13,16,13,17), (14,17,14,18)]
dc.SetPen(self.pen)
dc.DrawText(‘hello world’, 60,65)
for eachcoord in coords:
dc.DrawLine(*eachcoord)
dc.EndDrawing()

########################

On 7/11/06, Gabriel Murray gabriel.murray@gmail.com wrote:

Hello,

I am trying to use wxPython to draw some simple lines but am not
getting the results that I want. In my user interface, when the user
makes certain actions I want a window to pop up which will have certain
multi-coloured bars displaying some information to them.

This is the code I have for that window, with some sample coordinates
put in there. I get the window with a white background but no line. Do
I need to refresh it somehow after the line is drawn?

Thanks,

Gabriel

##############

class PopupFrame(wx.Frame):

def __init__(self):

    wx.Frame.__init__(self, None, -1, 'Status Windowt', size=(300,250))

    self.sketch = SketchWindow(self,-1)

class SketchWindow(wx.Window):

def __init__(self, parent, ID):

    wx.Window.__init__(self, parent, ID)

    self.SetBackgroundColour("White")

    self.color = "Black"

    self.thickness = 1

    self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)

    self.InitBuffer()





def InitBuffer(self):

    size = self.GetClientSize()

    self.buffer = wx.EmptyBitmap(size.width, size.height)

    dc = wx.BufferedDC(None, self.buffer)

    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))

    dc.Clear()

    self.DrawLines(dc)

    self.reInitBuffer = False





def DrawLines(self, dc):

    coords = [(12,13,12,14), (12,14,12,15), (12,15,12,16), (13,16,13,17), (14,17,14,18)]

    for eachcoord in coords:

        dc.DrawLine(*eachcoord)

http://wiki.wxpython.org/index.cgi/RecipesImagesAndGraphics

specifically:

http://wiki.wxpython.org/index.cgi/DoubleBufferedDrawing

The Wiki is a bit hard to navigate, but there's some good stuff there.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Gabriel Murray wrote:

As a follow-up on this, I've changed the code slighly here, but am still not
able to get lines drawn to the screen. Am I using the wrong type of dc? I've
not tried to draw lines with wxPython before, so am really not sure.

###########

class MyFrame(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self, None, -1, 'Global Context', size=(250,500))
       self.panel = MyPanel(self,-1)

class MyPanel(wx.Panel):

   def __init__(self, parent, id):
       wx.Panel.__init__(self, parent, id, size=(250,500))
       self.sketch = SketchWindow(self,-1)

class SketchWindow(wx.Window):
   def __init__(self, parent, ID):
       wx.Window.__init__(self, parent, ID, size=(250,500))
       self.SetBackgroundColour("Red")
       self.color = "White"
       self.thickness = 1
       self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)
       size = self.GetClientSize()
       self.buffer = wx.EmptyBitmap(size.width, size.height)
       dc = wx.BufferedDC(None, self.buffer)
       self.PrepareDC(dc)

I don't know for sure, but it seems that at this point, dc
is just an in-memory image, with no connection to any display
device. I would try

dc = wx.BufferedDC (wx.ClientDC (self), self.buffer)

which would seem to make your SketchWindow the destination
for changes to the BufferedDC.

I haven't tested this. (I've never played with BufferedDC.)
Usually I put such things into an OnPaint method bound to
wx.EVT_PAINT so that it will survive window moves, resizes, etc.

         Good Luck, Mel.
2