HOME/Articles/

tkinter example triangle angles (snippet)

Article Outline

Python tkinter (gui) example 'triangle angles'

Functions in program:

  • def distance(x1, y1, x2, y2):

Modules used in program:

  • import math

triangle angles

Python tkinter example: triangle angles

from tkinter import * # Import tkinter
from random import randint
import math

width = 300
height = 200
radius = 2  

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    # Is point (x, y) close to this point
    def isNearBy(self, x, y):
        return distance(self.x, self.y, x, y) < radius + 3

def distance(x1, y1, x2, y2):
    return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5

class MainGUI:
    def __init__(self):   
        window = Tk() # Create a window
        window.title("Triangle angles") # Set title

        self.canvas = Canvas(window, width = width, height = height)
        self.canvas.pack()

        self.p1 = Point(randint(0, width - 1), randint(0, height - 1))
        self.p2 = Point(randint(0, width - 1), randint(0, height - 1))
        self.p3 = Point(randint(0, width - 1), randint(0, height - 1))
        self.drawTriangle()

        self.canvas.bind("<B1-Motion>", self.mouseMoved)

        window.mainloop() # Create an event loop

    def mouseMoved(self, event):
        if self.closeToAPoint(event.x, event.y):
            self.canvas["cursor"] = "plus"
        else:
            self.canvas["cursor"] = "arrow"

        if self.p1.isNearBy(event.x, event.y):
            self.p1.x = event.x
            self.p1.y = event.y
        elif self.p2.isNearBy(event.x, event.y):
            self.p2.x = event.x
            self.p2.y = event.y
        elif self.p3.isNearBy(event.x, event.y):
            self.p3.x = event.x
            self.p3.y = event.y

        self.drawTriangle()

    def closeToAPoint(self, x, y):
        return self.p1.isNearBy(x, y) or self.p2.isNearBy(x, y) or self.p3.isNearBy(x, y)

    def drawTriangle(self):
        self.canvas.delete("line")
        self.canvas.create_line(self.p1.x, self.p1.y, self.p2.x, self.p2.y, tags = "line")
        self.canvas.create_line(self.p2.x, self.p2.y, self.p3.x, self.p3.y, tags = "line")
        self.canvas.create_line(self.p3.x, self.p3.y, self.p1.x, self.p1.y, tags = "line")

        a = distance(self.p2.x, self.p2.y, self.p3.x, self.p3.y)
        b = distance(self.p1.x, self.p1.y, self.p3.x, self.p3.y)
        c = distance(self.p1.x, self.p1.y, self.p2.x, self.p2.y)
        A = math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c)))
        B = math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c)))
        C = math.degrees(math.acos((c * c - b * b - a * a) / (-2 * a * b)))

        self.canvas.create_text(self.p1.x, self.p1.y, text = str(int(A)), tags = "line")
        self.canvas.create_text(self.p2.x, self.p2.y, text = str(int(B)), tags = "line")
        self.canvas.create_text(self.p3.x, self.p3.y, text = str(int(C)), tags = "line")

MainGUI()