Four lines are drawn in different styles. We see how attributes like color and end shape can be obtained. A Python for loop
is used to make an interesting pattern using the specifications of the dash attribute. In addition the color of the canvas background has been made green.
The instructions used in recipe 1 should be used again.
Just use the name 4lines.py
when you write, save, and execute this program.
Arrows and endcaps have been introduced into the line specifications.
#4lines.py #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> from Tkinter import * root = Tk() root.title('Different line styles') cw = 280 # canvas width ch = 120 # canvas height canvas_1 = Canvas(root, width=cw, height=ch, background="spring \ green") canvas_1.grid(row=0, column=1) x_start, y_start = 20, 20 x_end, y_end = 180, 20 canvas_1.create_line(x_start, y_start, x_end,y_end,\ dash=(3,5), arrow="first", width = 3) x_start, y_start = x_end, y_end x_end, y_end = 50, 70 canvas_1.create_line(x_start, y_start, x_end,y_end,\ dash=(9,5), width = 5, fill= "red") x_start, y_start = x_end, y_end x_end, y_end = 150, 70 canvas_1.create_line(x_start, y_start, x_end,y_end, \ dash=(19,5),width= 15, caps="round", \fill= "dark blue") x_start, y_start = x_end, y_end x_end, y_end = 80, 100 canvas_1.create_line(x_start, y_start, x_end,y_end, fill="purple") #width reverts to default= 1 in absence of explicit spec. root.mainloop() #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
To draw a line you only need to give the start point and the end point.
The preceding screenshot shows the result of execution on Ubuntu Linux
In this example we have saved a bit of work by re-using previous line position specifications. See the next two screenshots.
The preceding screenshot shows the result of execution on MS Windows XP.
Here is where you may see the difference between Linux's and MS Windows's ability to draw dashed lines using Tkinter. The solid portion of the dash was specified as 19 pixels long. On the Linux (Ubuntu9.10) platform this specification was respected but Windows disregarded the instruction.