我正在编写一个python脚本作为创建转速计的概念证明。
我正在测试tkinter画一条线,其中针将在转速计上。但是,当我绘制时它不会立即显示,但我需要它。
此外,我相信我可能会失去理智,因为我无法在我的代码中找到中心线的位置,它位于我的TachoMeter类中,但我似乎无法找到它。
无论如何,这是代码:
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()
非常感谢任何帮助。我在Python方面仍然很陌生,但它将是一个有趣的项目。
谢谢, 布雷特