FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 18.4 | Next Entry >>

18.4. How do I draw dashed or dotted lines in a GtkDrawingArea?

To draw dashes, you can manipulate the line style attribute of the DrawingArea's GC object before drawing the lines. Constant values from GDK.py are:

 LINE_SOLID
 LINE_ON_OFF_DASH
 LINE_DOUBLE_DASH
So you can do something like:

 line_style = GDK.LINE_ON_OFF_DASH
 gc.line_style(line_style)
For dots, John Finlay writes: "You can create your own dash pattern using set_dashes()." I've added information here from the tutorial at [www.moeraki.com] :

"To define a dash pattern (taking notice that the line_style must be set to LINE_ON_OFF_DASH or LINE_DOUBLE_DASH), use the following GC method:

 gc.set_dashes(offset, dash_list)
where offset is the index of the starting dash value in dash_list and dash_list is a list or tuple containing numbers of pixels to be drawn or skipped to form the dashes. The dashes are drawn starting with the number of pixels at the offset position; then the next number of pixels is skipped; and then the next number is drawn; and so on rotating through all the dash_list numbers and starting over when the end is reached. For example, if the dash_list is (2, 4, 8, 16) and the offset is 1, the dashes will be drawn as: draw 4 pixels, skip 8 pixels, draw 16 pixels, skip 2 pixels, draw 4 pixels and so on."

John Hunter shows us how dots can be done in practice:

 self._spacing = 2
 gc.line_style = GDK.LINE_ON_OFF_DASH
 gc.cap_style =  GDK.CAP_BUTT
 gc.join_style = GDK.JOIN_ROUND
 gc.set_dashes(0,[1,self._spacing])
 widget.draw_lines(gc, zip(x, y) )

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org