Is there any way to draw a line with arrow in wx?

561 views
Skip to first unread message

Shannon

unread,
Feb 21, 2013, 10:44:45 AM2/21/13
to wxpytho...@googlegroups.com
Hi, 
I was wondering is there any way to draw a line with an arrow, which can indicate the from 'point 1' point to 'point 2'.
Does anyone know how to implement this?

Thanks
Shannon

Vlastimil Brom

unread,
Feb 21, 2013, 4:21:47 PM2/21/13
to wxpytho...@googlegroups.com
2013/2/21 Shannon <shanno...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to wxpython-user...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Hi,
I haven't used any of these myself, but OGL and FloatCanvas come to
mind in this context; see the wxpython demo:
More windows/controls: FloatCanvas
Miscelaneous: OGL

There are likely some other graphic libraris, with this capabilitry.

hth,
vbr

Bruce Sherwood

unread,
Feb 21, 2013, 4:31:55 PM2/21/13
to wxpytho...@googlegroups.com
Probably overkill, but VPython (vpython.org), which is now based on wxPython, makes it extremely easy to draw a 3D arrow. The single statement

arrow()

draws a default white arrow with its tail at the origin, pointing to the right, with length 1. You can optionally specify a tail position, a direction and length, and a color:

A = arrow(pos=(1,2,-1), axis(5,2,3), color=color.cyan)

Later in the program you can alter any of these attributes. For example,

A.color = color.red

With the mouse you can zoom and rotate the "camera" that is viewing the 3D scene.

Chris Barker - NOAA Federal

unread,
Feb 22, 2013, 12:06:08 PM2/22/13
to wxpytho...@googlegroups.com
On Thu, Feb 21, 2013 at 1:21 PM, Vlastimil Brom
<vlastim...@gmail.com> wrote:
> 2013/2/21 Shannon <shanno...@gmail.com>:
>> I was wondering is there any way to draw a line with an arrow, which can
>> indicate the from 'point 1' point to 'point 2'.

> I haven't used any of these myself, but OGL and FloatCanvas come to
> mind in this context; see the wxpython demo:
> More windows/controls: FloatCanvas

Indeed -- for FloatCanvas, look at the main Demo, look at the "Arrows"
demo under the "Tests" Menu - the arrows are not particularly pretty,
but you could make your own DrawObject with prettier ones if you want.

There are also bunch more little FloatCanvas demos in SVN also:

http://svn.wxwidgets.org/svn/wx/wxPython/3rdParty/FloatCanvas/Demos/

The VectPlot.py demo uses arrows.

-Chris


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Edward Sitarski

unread,
Sep 11, 2021, 11:55:12 AM9/11/21
to wxPython-users
I realize this question was asked a long time ago.
Sometimes you simply want to draw an arrow in a dc.
I recently ran into this problem.  See below:

from math import atan2, sin, cos, pi
def DrawArrowLine( dc, x0, y0, x1, y1, arrowFrom=True, arrowTo=True, arrowLength=16, arrowWidth=8 ):
    '''
        Draws a line with arrows in a regular wxPython DC.
        The line is drawn with the dc's wx.Pen.  The arrows are filled with the current Pen's colour.
        Edward Sitarski 2021.
    '''
    dc.DrawLine( x0, y0, x1, y1 )
    if x0 == x1 and y0 == y1:
        return
    
    # Set up the dc for drawing the arrows.
    penSave, brushSave = dc.GetPen(), dc.GetBrush()
    dc.SetPen( wx.TRANSPARENT_PEN )
    dc.SetBrush( wx.Brush(dc.GetPen().GetColour()) )
    
    # Compute the "to" arrow polygon.
    angle = atan2( y1 - y0, x1 - x0 )
    toCosAngle, toSinAngle = -cos(angle), -sin(angle)
    toArrowPoly = [
        (int(xp*toCosAngle - yp*toSinAngle), int(yp*toCosAngle + xp*toSinAngle)) for xp, yp in (
            (0,0),
            (arrowLength, arrowWidth/2),
            (arrowLength, -arrowWidth/2),
        )
    ]
    
    # Draw the arrows.
    if arrowTo:
        dc.DrawPolygon( toArrowPoly, x1, y1 )
    if arrowFrom:
        dc.DrawPolygon( [(-x,-y) for x,y in toArrowPoly], x0, y0 )
    
    # Restore the dc.
    dc.SetPen( penSave )
    dc.SetBrush( brushSave )

Edward Sitarski

unread,
Sep 11, 2021, 12:00:10 PM9/11/21
to wxPython-users
Deepest appologies.   The above code has a bug.  Please use the following:

from math import atan2, sin, cos, pi
def DrawArrowLine( dc, x0, y0, x1, y1, arrowFrom=True, arrowTo=True, arrowLength=16, arrowWidth=8 ):
    '''
        Draws a line with arrows in a regular wxPython DC.
        The line is drawn with the dc's wx.Pen.  The arrows are filled with the current Pen's colour.
        Edward Sitarski 2021.
    '''
    dc.DrawLine( x0, y0, x1, y1 )
    if x0 == x1 and y0 == y1:
        return
    
    # Set up the dc for drawing the arrows.
    penSave, brushSave = dc.GetPen(), dc.GetBrush()
    dc.SetPen( wx.TRANSPARENT_PEN )
    dc.SetBrush( wx.Brush(penSave.GetColour()) )
    
    # Compute the "to" arrow polygon.
    angle = atan2( y1 - y0, x1 - x0 )
    toCosAngle, toSinAngle = -cos(angle), -sin(angle)
    toArrowPoly = [
        (int(xp*toCosAngle - yp*toSinAngle), int(yp*toCosAngle + xp*toSinAngle)) for xp, yp in (
            (0,0),
            (arrowLength, arrowWidth/2),
            (arrowLength, -arrowWidth/2),
        )
    ]
    
    # Draw the arrows.
    if arrowTo:
        dc.DrawPolygon( toArrowPoly, x1, y1 )
    if arrowFrom:
        dc.DrawPolygon( [(-x,-y) for x,y in toArrowPoly], x0, y0 )
    
    # Restore the dc.
    dc.SetPen( penSave )
    dc.SetBrush( brushSave )


Reply all
Reply to author
Forward
0 new messages