• DDA Line Drawing Tkinter-Python

    DDA Line Drawing Tkinter-Python

    In this DDA Line Drawing Tkinter-Python Post, Tkinter enables you to develop GUI programs and is an excellent pedagogical tool for learning object-oriented programming.

    There are many GUI modules available for developing GUI programs in Python. We have used the turtle module for drawing geometric shapes in Computer Graphics. Turtle is easy to use and is an effective pedagogical tool for introducing the fundamentals of programming to beginners.

    However, you cannot use turtle to create graphical user interfaces. We introduce Tkinter, which will enable you to develop Graphical programs using canvas and GUI components. Tkinter is not only a useful tool for developing GUI projects, but it is also a valuable pedagogical tool for learning Object-Oriented programming.

    Using Python Tkinter, The DDA [Digital Differential Analyzer] Line Draw Algorithm is used to draw a line between two points having their co-ordinates. This algorithm is converted into python program using python GUI Tkinter. The user needs to enter co-ordinates for two points and click on “draw Line” button; the line is graphically drawn on to canvas.


    # Import all definitions from tkinter

    from tkinter import *

    class DDALineDrawTk:

    DDA Line Drawing

    DDA Line Drawing using Tkinter

    # Implement DDA Line Draw Algorithm

    def drawLine(self):

    x1 = int(self.x1Var.get())

    x2 = int(self.x2Var.get())

    y1 = eval(self.y1Var.get())

    y2 = eval(self.y2Var.get())

    dx = abs(x2 – x1)

    dy = abs(y2 – y1)

    length = dx if dx >= dy else dy

    dx = int((x2 – x1)/length)

    dy = int((y2 – y1)/length)

    x = x1

    y = y1



    x = x + dx

    y = y + dy

    self.canvas.create_text(x, y, text = “.”, tag = “line”)

    i = i + 1

    # To Clear Canvas

    def clearCanvas(self):


    # Class Initial Method Calls Automatically as a constructor

    def __init__(self):

    window = Tk() # Create a window

    window.title(“DDA Line Draw Program”) # Set Title

    # Place canvas in the window

    self.canvas = Canvas(window, width = 500, height = 500, bg = “white”)


    # Draw Frame in the window

    frame = Frame(window)


    Label(frame, text=”Enter (x1, y1) : “).grid(row = 1, column = 1)

    self.x1Var = StringVar()

    Entry(frame, textvariable = self.x1Var).grid(row = 1, column = 2)

    self.y1Var = StringVar()

    Entry(frame, textvariable = self.y1Var).grid(row = 1, column = 3)

    Label(frame, text=”Enter (x2, y2) : “).grid(row = 2, column = 1)

    self.x2Var = StringVar()

    Entry(frame, textvariable = self.x2Var).grid(row = 2, column = 2)

    self.y2Var = StringVar()

    Entry(frame, textvariable = self.y2Var).grid(row = 2, column = 3)

    btnDrawLine = Button(frame, text = “Draw Line”, command = self.drawLine)

    btnDrawLine.grid(row = 3, column = 1)

    btnClear = Button(frame, text = “Clear Canvas”, command = self.clearCanvas)

    btnClear.grid(row = 3, column = 2)

    window.mainloop() # Create an event loop

    # Program is run from here by creating instance of Class


    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *