gpt4 book ai didi

Python TachoMeter,如何在每个 create_line 之后显示

转载 作者:行者123 更新时间:2023-12-01 03:33:47 26 4
gpt4 key购买 nike

我正在编写一个 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 还很陌生,但这将是一个有趣的项目。

谢谢,布雷特

最佳答案

我的解决方案:

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

关于Python TachoMeter,如何在每个 create_line 之后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com