Python 2.6 Graphics Cookbook
上QQ阅读APP看书,第一时间看更新

Draw a dashed line

A straight dashed line, three pixels thick is drawn.

How to do it...

The instructions used in the previous example are used. The only change is in the name of the Python program. This time you should use the name dashed_line.py instead of line_1.py.

# dashed_line.py
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
from Tkinter import *
root = Tk()
root.title('Dashed line')
cw = 800                                      # canvas width
ch = 200                                      # canvas height
canvas_1 = Canvas(root, width=cw, height=ch)
canvas_1.grid(row=0, column=1)

x_start = 10
y_start = 10
x_end = 500
y_end = 20
canvas_1.create_line(x_start, y_start, x_end,y_end,dash=(3,5), width = 3)
root.mainloop()#

How it works...

The new things here are the addition of some style specifications for the line.

dash=( 3,5) says that there should be three solid pixels followed by five blank pixels and width = 3 specifies that the line should be 3 pixels thick.

There's more...

You can specify a limitless variety of dash-space patterns. A dash-space pattern specified as dash = (5, 3, 24, 2, 3, 11) would result in a line with three patterns repeated over and over throughout the length of the line. The pattern would consist of five solid pixels followed by three blank pixels. Then there would be 24 solid pixels followed by only two blank pixels. The third variation would be three solid followed by 11 blank pixels and then the whole set of three patterns would begin again. The list of dash-blank pairs can go on as long as you like. The even-numbered length specifications will specify the length of solid pixels.

Note

The dash attribute is quirky on different operating systems. For instance on a Linux operating system it behaves as it should by obeying the directives for line and space distances but on MS Windows there is no respect for solid-dash directives if they exceed ten pixels in size