Python

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • drawing lines

    5 answers - 4461 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Is this heading in the right direction? RIght now I'm not getting any output
    at all, but am wondering if this is basically the right idea.
    Thanks,
    Gabriel
    import wx
    class MyFrame(wx.Frame):
    def __init__(self):
    wx.Frameinit__(self, None, -1, 'Global Context', size=(250,500))
    self.panel = wx.Panelinit__(self,parent,id, size=(250,500))
    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    EVT_PAINT(self.buffer, self.onPaint)
    def onPaint(self):
    dc = wx.BufferedPaintDC(None, self.buffer)
    self.PrepareDC(dc)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour ()))
    dc.Clear()
    dc.BeginDrawing()
    dc.SetPen(self.pen)
    dc.DrawText('hello world', 60,65)
    dc.DrawLine(20,20,30,30)
    dc.EndDrawing()
    app = wx.PySimpleApp(redirect='True')
    frm = MyFrame()
    frm.SetSize((250,500))
    frm.Show()
    app.SetTopWindow(frm)
    app.MainLoop()
    7/11/06, Lanier, Paul <Paul.Lanier (AT) analog (DOT) comwrote:
    Put the drawing in a EVT_PAINT handler and use a wx.PaintDC or
    wx.BufferedPaintDC.
    -Paul
    *From:* Gabriel Murray [mailto:gabriel.murray (AT) gmail (DOT) com]
    *Sent:* Tuesday, July 11, 2006 11:16 AM
    *To:* wxPython-users (AT) lists (DOT) wxwidgets.org
    *Subject:* [wxPython-users] Re: drawing lines
    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.Frameinit__(self, None, -1, 'Global Context',
    size=(250,500))
    self.panel = MyPanel(self,-1)
    class MyPanel(wx.Panel):
    def __init__(self, parent, id):
    wx.Panelinit__(self, parent, id, size=(250,500))
    self.sketch = SketchWindow(self,-1)
    --
    class SketchWindow(wx.Window):
    def __init__(self, parent, ID):
    wx.Windowinit__(self, parent, ID, size=(250,500))
    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    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()
    --
    7/11/06, Gabriel Murray <gabriel.murray (AT) gmail (DOT) comwrote:
    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.Frameinit__(self, None, -1, 'Status Windowt',
    size=(300,250))
    self.sketch = SketchWindow(self,-1)
    --
    class SketchWindow(wx.Window):
    def __init__(self, parent, ID):
    wx.Windowinit__(self, parent, ID)
    self.SetBackgroundColour("White")
    self.color = "Black"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    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)
    >
    >
    >
  • No.1 | | 3214 bytes | |

    Put the drawing in a EVT_PAINT handler and use a wx.PaintDC or
    wx.BufferedPaintDC.
    -Paul

    From: Gabriel Murray [mailto:gabriel.murray (AT) gmail (DOT) com]
    Sent: Tuesday, July 11, 2006 11:16 AM
    To: wxPython-users (AT) lists (DOT) wxwidgets.org
    Subject: [wxPython-users] Re: drawing lines

    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.Frameinit__(self, None, -1, 'Global Context',
    size=(250,500))
    self.panel = MyPanel(self,-1)

    class MyPanel(wx.Panel):

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

    class SketchWindow(wx.Window):
    def __init__(self, parent, ID):
    wx.Windowinit__(self, parent, ID, size=(250,500))
    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    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()

    7/11/06, Gabriel Murray <gabriel.murray (AT) gmail (DOT) comwrote:

    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.Frameinit__(self, None, -1, 'Status
    Windowt', size=(300,250))
    self.sketch = SketchWindow(self,-1)

    class SketchWindow(wx.Window):
    def __init__(self, parent, ID):
    wx.Windowinit__(self, parent, ID)
    self.SetBackgroundColour("White")
    self.color = "Black"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness,
    wx.SLID)
    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)
  • No.2 | | 659 bytes | |

    Gabriel Murray wrote:
    Is this heading in the right direction? RIght now I'm not getting any
    output at all, but am wondering if this is basically the right idea.

    You're close. You want to bind the EVT_PAINT to the panel, not the
    buffer. The buffer is a wx.Bitmap, which doesn't get any events.

    self.panel.Bind(EVT_PAINT, self.onPaint)

    However, your first approach was better. You did all the drawing to a
    buffer in a separate method. All you needed to do was draw the buffer to
    the Screen in the onPaint method.

    See the Wiki entries I pointed you to in a previous post, and also this one:

    -Chris
  • No.3 | | 1286 bytes | |

    Gabriel Murray wrote:
    Is this heading in the right direction? RIght now I'm not getting any
    output at all, but am wondering if this is basically the right idea.
    Thanks,
    Gabriel

    import wx

    class MyFrame(wx.Frame):
    def __init__(self):
    wx.Frameinit__(self, None, -1, 'Global Context', size=(250,500))
    self.panel = wx.Panelinit__(self,parent,id, size=(250,500))
    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    EVT_PAINT(self.buffer, self.onPaint)

    Using self.buffer is wrong. Also since it is the panel that will need
    painting, you need to bind the panel's paint event to the handler.

    self.panel.Bind(wx.EVT_PAINT, self.onPaint)

    def onPaint(self):

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

    You need to tell the dc which window to draw to.

    dc = wx.BufferedPaintDC(self.panel, self.buffer)

    You'll also want to reinitialize the buffer whenever the window size
    changes, so do that in a EVT_SIZE handler.

    Take a look at the doodle.py module in samples/doodle for more detailed
    sample code.
  • No.4 | | 2649 bytes | |

    Thanks for all your suggestions. I've made the changes but am not getting
    any output at all, not even an empty frame. The code is below. In the line

    dc = wx.BufferedPaintDC(self.panel, self.buffer)

    I wasn't sure if it should be self.panel or just self, but either way I get
    no output.

    import wx

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

    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    self.panel.Bind(wx.EVT_PAINT, self.onPaint)

    def onPaint(self, evt):
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    dc = wx.BufferedPaintDC(self.panel, self.buffer)
    self.PrepareDC(dc)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour ()))
    dc.Clear()
    dc.BeginDrawing()
    dc.SetPen(self.pen)
    dc.DrawText('hello world', 60,65)
    dc.EndDrawing()

    app = wx.PySimpleApp(redirect='True')

    frm = MyFrame()
    frm.SetSize((250,500))
    frm.Show()

    app.SetTopWindow(frm)
    app.MainLoop()

    7/11/06, Robin Dunn <robin (AT) alldunn (DOT) comwrote:

    Gabriel Murray wrote:
    Is this heading in the right direction? RIght now I'm not getting any
    output at all, but am wondering if this is basically the right idea.
    Thanks,
    Gabriel

    import wx

    class MyFrame(wx.Frame):
    def __init__(self):
    wx.Frameinit__(self, None, -1, 'Global Context',
    size=(250,500))
    self.panel = wx.Panelinit__(self,parent,id, size=(250,500))
    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    EVT_PAINT(self.buffer, self.onPaint)

    Using self.buffer is wrong. Also since it is the panel that will need
    painting, you need to bind the panel's paint event to the handler.

    self.panel.Bind(wx.EVT_PAINT, self.onPaint)
    --
    def onPaint(self):

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

    You need to tell the dc which window to draw to.

    dc = wx.BufferedPaintDC(self.panel, self.buffer)

    You'll also want to reinitialize the buffer whenever the window size
    changes, so do that in a EVT_SIZE handler.

    Take a look at the doodle.py module in samples/doodle for more detailed
    sample code.
    --
  • No.5 | | 2873 bytes | |

    Ah-ha, got it sorted. Thanks, all!

    7/12/06, Gabriel Murray <gabriel.murray (AT) gmail (DOT) comwrote:

    Thanks for all your suggestions. I've made the changes but am not getting
    any output at all, not even an empty frame. The code is below. In the line
    --
    dc = wx.BufferedPaintDC(self.panel, self.buffer)

    I wasn't sure if it should be self.panel or just self, but either way I
    get no output.
    --

    import wx

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

    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness, wx.SLID)
    self.panel.Bind(wx.EVT_PAINT, self.onPaint)

    def onPaint(self, evt):

    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    dc = wx.BufferedPaintDC(self.panel, self.buffer)
    self.PrepareDC(dc)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour ()))
    dc.Clear()
    dc.BeginDrawing()
    dc.SetPen(self.pen)
    dc.DrawText('hello world', 60,65)
    dc.EndDrawing()
    >
    >
    >
    >

    app = wx.PySimpleApp(redirect='True')

    frm = MyFrame()
    frm.SetSize((250,500))
    frm.Show()

    app.SetTopWindow(frm)
    app.MainLoop()

    >
    >
    >

    7/11/06, Robin Dunn <robin (AT) alldunn (DOT) comwrote:

    Gabriel Murray wrote:
    Is this heading in the right direction? RIght now I'm not getting any
    output at all, but am wondering if this is basically the right idea.
    Thanks,
    Gabriel

    import wx

    class MyFrame(wx.Frame):
    def __init__(self):
    wx.Frameinit__(self, None, -1, 'Global Context',
    size=(250,500))
    self.panel = wx.Panelinit_ _(self,parent,id,
    size=(250,500))
    self.SetBackgroundColour("Red")
    self.color = "White"
    self.thickness = 1
    self.pen = wx.Pen(self.color, self.thickness , wx.SLID)
    size = self.GetClientSize()
    self.buffer = wx.EmptyBitmap(size.width, size.height)
    EVT_PAINT(self.buffer, self.onPaint)

    Using self.buffer is wrong. Also since it is the panel that will need
    painting, you need to bind the panel's paint event to the handler.

    self.panel.Bind(wx.EVT_PAINT, self.onPaint)
    --
    def onPaint(self):

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

    You need to tell the dc which window to draw to.

    dc = wx.BufferedPaintDC(self.panel, self.buffer)

    You'll also want to reinitialize the buffer whenever the window size
    changes, so do that in a EVT_SIZE handler.

    Take a look at the doodle.py module in samples/doodle for more detailed
    sample code.
    --

Re: drawing lines


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "Python"

EMSDN.COM