Brett Reinhard Brett Reinhard - 25 days ago 9
Python Question

Python TachoMeter, How to display after each create_line

I am writing a python script as a proof of concept of creating a tachometer.

I am testing with tkinter to draw a line where the needle would be on a tachometer. However, when I draw it doesn't display right away, but I need it to.

Additionally, I believe I may be losing my mind as I cannot find where the center line is coming from in my code, it is somewhere in my TachoMeter class but I just cant seem to find it.

Anyway, here is the code:

import Tkinter as tk
import math
import time


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

class TachoMeter(tk.Tk):
def __init__(self, CenterPoint = Point(), LeftPoint=Point(), Radius=0, LeftBuffer=0, BottomBuffer=0, MIN_RPM=0, MAX_RPM=0, canvas_height=0, canvas_width=0, *arg, **kwargs):
tk.Tk.__init__(self, *arg, **kwargs)
self.canvas = tk.Canvas(width=canvas_width, height=canvas_height)
self.canvas.pack(fill="both", expand=True)
self.CenterPoint = CenterPoint
self.MAX_RPM = MAX_RPM
self.MIN_RPM = MIN_RPM
self.LeftBuffer = LeftBuffer
self.BottomBuffer = BottomBuffer
self.Radius = Radius
self.Point_0 = LeftPoint
self._create_arc((LeftPoint.x,LeftPoint.y), (LeftPoint.x + 2*Radius,LeftPoint.y))




def _create_arc(self, p0, p1):
extend_x = (self._distance(p0,p1) -(p1[0]-p0[0]))/2
extend_y = (self._distance(p0,p1) -(p1[1]-p0[1]))/2
startAngle = math.atan2(p0[0] - p1[0], p0[1] - p1[1]) *180 / math.pi
self.canvas.create_arc(p0[0]-extend_x, p0[1]-extend_y , p1[0]+extend_x, p1[1]+extend_y, extent=180, start=90+startAngle)
self.canvas.create_arc(LeftPoint.x+Radius,LeftPoint.y,LeftPoint.x+Radius,LeftPoint.y-Radius)

def _distance(self, p0, p1):
return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)


def _rpmGauge():


for x in range(180,0,-1):
curTheta = float((math.pi*x)/180)
#print(int(curTheta))
curX = TACH.CenterPoint.x +(TACH.Radius*math.cos(curTheta))
#print(int(curX))
curY = TACH.CenterPoint.y - (TACH.Radius*math.sin(curTheta))
#print(int(curY))
TACH.canvas.create_line(TACH.CenterPoint.x,TACH.CenterPoint.y,int(curX),int(curY))
TACH.canvas.pack()
time.sleep(0.025)






Radius = 400
acanvas_width = 500
acanvas_height = 600
LeftBuffer = 100
BottomBuffer = 100
CenterPoint = Point(LeftBuffer+Radius,acanvas_height - BottomBuffer)
LeftPoint = Point(LeftBuffer,acanvas_height-BottomBuffer)
MAX_RPM = 8000
MIN_RPM = 0


TACH = TachoMeter(CenterPoint,LeftPoint,Radius,LeftBuffer,BottomBuffer, MIN_RPM, MAX_RPM, acanvas_height, acanvas_width)

TACH.after(2000, _rpmGauge)
TACH.mainloop()


Any help would be greatly appreciated. I am still very new at Python, but though it would be a fun project to take on.

Thanks,
Brett

Answer

My solution:

import Tkinter as tk
import math
import time


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


class TachoMeter(tk.Tk):
    def __init__(self, CenterPoint=Point(), LeftPoint=Point(), Radius=0, LeftBuffer=0, BottomBuffer=0, MIN_RPM=0,
                 MAX_RPM=0, canvas_height=0, canvas_width=0, *arg, **kwargs):
        tk.Tk.__init__(self, *arg, **kwargs)
        self.canvas = tk.Canvas(width=canvas_width, height=canvas_height)
        self.canvas.pack(fill="both", expand=True)
        self.CenterPoint = CenterPoint
        self.MAX_RPM = MAX_RPM
        self.MIN_RPM = MIN_RPM
        self.LeftBuffer = LeftBuffer
        self.BottomBuffer = BottomBuffer
        self.Radius = Radius
        self.Point_0 = LeftPoint
        self._create_arc((LeftPoint.x, LeftPoint.y), (LeftPoint.x + 2 * Radius, LeftPoint.y))
        self._rpmGauge()

    def _create_arc(self, p0, p1):
        extend_x = (self._distance(p0, p1) - (p1[0] - p0[0])) / 2
        extend_y = (self._distance(p0, p1) - (p1[1] - p0[1])) / 2
        startAngle = math.atan2(p0[0] - p1[0], p0[1] - p1[1]) * 180 / math.pi
        self.canvas.create_arc(p0[0] - extend_x, p0[1] - extend_y, p1[0] + extend_x, p1[1] + extend_y, extent=180,
                               start=90 + startAngle)
        self.canvas.create_arc(LeftPoint.x + Radius, LeftPoint.y, LeftPoint.x + Radius, LeftPoint.y - Radius)

    def _distance(self, p0, p1):
        return math.sqrt((p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2)

    def _rpmGauge(self):
        for x in range(180, 0, -1):
            curTheta = float((math.pi * x) / 180)
            curX = self.CenterPoint.x + (self.Radius * math.cos(curTheta))
            curY = self.CenterPoint.y - (self.Radius * math.sin(curTheta))
            self.line = self.canvas.create_line(self.CenterPoint.x, self.CenterPoint.y, int(curX), int(curY))
            self.canvas.pack()


Radius = 400
acanvas_width = 500
acanvas_height = 600
LeftBuffer = 100
BottomBuffer = 100
CenterPoint = Point(LeftBuffer + Radius, acanvas_height - BottomBuffer)
LeftPoint = Point(LeftBuffer, acanvas_height - BottomBuffer)
MAX_RPM = 8000
MIN_RPM = 0

TACH = TachoMeter(CenterPoint, LeftPoint, Radius, LeftBuffer, BottomBuffer, MIN_RPM, MAX_RPM, acanvas_height,
                  acanvas_width)

TACH.mainloop()

enter image description here