Python Tkinter | Create different type of lines using Canvas class

In Tkinter, Canvas.create_line() method is used to create lines in any canvas. These lines can only be seen on canvas so first you need to create a Canvas object and later pack it to the main window.

Syntax:

Canvas.create_line(x1, y1, x2, y2, ...., options = ...)

Note: Minimum 4 points are required to create a line, but you can also add multiple points to create different drawings.

Class paramaters –

Data members used:
master
canvas

Member functions used for the given class: create() method

Widgets used: Canvas

Tkinter method used:
canvas.create_line()
pack()
title()
geometry()

Below is the Python code –

filter_none

edit
close

play_arrow

link
brightness_4
code

# Imports each and every method and class 
# of module tkinter and tkinter.ttk
from tkinter import *
from tkinter.ttk import * 
  
class GFG:
    def __init__(self, master = None):
        self.master = master
  
        # Calls create method of class GFG
        self.create()
  
    def create(self):
  
        # This creates a object of class canvas
        self.canvas = Canvas(self.master)
  
        # This creates a line of length 200 (straight horizontal line)
        self.canvas.create_line(15, 25, 200, 25)
  
        # This creates a lines of 300 (straight vertical dashed line)
        self.canvas.create_line(300, 35, 300, 200, dash = (5, 2))
          
        # This creates a triangle (triangle can be created by other methods also)
        self.canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85)
          
        # This pack the canvas to the main window and make it expandable
        self.canvas.pack(fill = BOTH, expand = True)
  
if __name__ == "__main__":
      
    # object of class Tk, resposible for creating
    # a tkinter toplevel window
    master = Tk()
    geeks = GFG(master)
  
    # This sets the title to Lines
    master.title("Lines")
  
    # This sets the geometry and position of window
    # on the screen
    master.geometry("400x250 + 300 + 300")
  
    # Infnite loop breaks only by interrupt
    master.mainloop()

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.