Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 9 - GUI Programming using Tkinter - Section 9.5 - Canvas - Check Point - MyProgrammingLab - Page 283: 9.19

Answer

You can draw a line with an arrowhead in Tkinter by using the create_line method and setting the arrow option to last or first. The last option will draw an arrowhead at the end of the line, while the first option will draw an arrowhead at the start of the line.

Work Step by Step

import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, bg="white", height=200, width=200) canvas.pack() x1, y1 = 10, 10 x2, y2 = 80, 80 canvas.create_line(x1, y1, x2, y2, arrow=tk.LAST, fill="red") root.mainloop() In this code, we first create the main window of the Tkinter application using the Tk() method and assign it to the variable root. Next, we create a canvas using the Canvas constructor, specifying the parent window as root and setting the background color to "white", the height to 200, and the width to 200. Then, we use the pack() method to display the canvas on the screen. Next, we specify the coordinates for the start and end points of the line as (10, 10) and (80, 80) respectively. Finally, we use the create_line method of the canvas to draw a line between the specified points and set the arrow option to tk.LAST. This will cause an arrowhead to be drawn at the end of the line with the specified color. You can also specify the shape and size of the arrowhead using the arrowshape option. The default arrow shape is (8, 10, 3), which represents the width, height, and offset of the arrowhead. You can adjust these values to create a different arrow shape.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.