绘制和更新车速表针

时间:2017-07-10 13:07:38

标签: python tkinter trigonometry

我正在尝试使用Python中的Tkinter Canvas绘制速度表,并且我的代码存在一些问题,而我似乎无法弄清楚。首先,这是我写的:

import tkinter as tk
from tkinter import ttk
import math

class DrawMeter(tk.Canvas):

    def __init__(self, parent, *args, **kwargs):
        tk.Canvas.__init__(self, parent, *args, **kwargs)
        self.config(bg = "grey")
        if (int(self['height']) * 2 > int(self['width'])):
            boxSide = int(self['width'])
        else:
            boxSide = int(self['height']) * 2
        self.boxX = boxSide / 2
        self.boxY = boxSide / 2
        self.boxRadius = int(0.40 * float(boxSide))
        self.start = 0
        self.end = 1

        self.drawBackground()
        self.drawTicks()
        self.drawNeedle()

    def drawBackground(self):
        bgColour = "black"
        self.create_arc((self.boxX - self.boxRadius,
                         self.boxY - self.boxRadius,
                         self.boxX * 4,
                         self.boxY * 4),
                        fill = bgColour, start = 90)

    def drawTicks(self):
        length = self.boxRadius / 8
        for deg in range(5, 85, 6):
            rad = math.radians(deg)
            self.Tick(rad, length)
        for deg in range(5, 91, 18):
            rad = math.radians(deg)
            self.Tick(rad, length * 2)

    def Tick(self, angle, length):
        cos = math.cos(angle)
        sin = math.sin(angle)
        radius = self.boxRadius * 2
        X = self.boxX * 2
        Y = self.boxY * 2
        self.create_line((X - radius * cos,
                          Y - radius * sin,
                          X - (radius - length) * cos,
                          Y - (radius - length) * sin),
                         fill = "white", width = 2)

    def drawText(self, start = 0, end = 100):
        interval = end / 5
        value = start
        length = self.boxRadius / 2
        for deg in range(5, 91, 18):
            rad = math.radians(deg)
            cos = math.cos(rad)
            sin = math.sin(rad)
            radius = self.boxRadius * 2
            self.create_text(self.boxX * 2 - (radius - length - 1) * cos,
                             self.boxY * 2 - (radius - length - 1) * sin,
                             text = str("{0:.1f}".format(value)),
                             fill = "white",
                             font = ("Arial", 12, "bold"))
            value = value + interval

    def setRange(self, start, end):
        self.start = start
        self.end = end
        self.drawText(start, end)

    def drawNeedle(self):
        X = self.boxX * 2
        Y = self.boxY * 2
        length = self.boxRadius - (self.boxRadius / 4)
        self.meterHand = self.create_line(X / 2, Y / 2, X + length, Y + length,
                                          fill = "red", width = 4)
        self.create_arc(X - 30, Y - 30, X + 30, Y + 30,
                        fill = "#c0c0c0", outline = "#c0c0c0", start = 90)

    def updateNeedle(self, value):
        length = self.boxRadius - (self.boxRadius / 4)
        deg = 80 * (value - self.start) / self.end - 180
        rad = math.radians(deg)
        self.coords(self.meterHand, self.boxX * 2, self.boxY * 2,
                    self.boxX + length * math.cos(rad),
                    self.boxY + length * math.sin(rad))

value = 0

def update_frame():
    global value
    if value < 1:
        value = value + 0.01
    print(value)
    meter.updateNeedle(value)
    container.after(200, update_frame)

root = tk.Tk()
container = tk.Frame(root)
container.pack()
meter = DrawMeter(container, height = 200, width = 200, bg = "red")
meter.setRange(0, 1)
meter.pack()
update_frame()
root.mainloop()

所以我遇到的问题是我的针在屏幕上正确绘制和更新。当我启动程序时,针头开始大约0.2ish,直到大约0.6然后停止。我觉得我计算针头位置的公式是错误的,但我不确定这是错误的。

我设置它的方式是,它取总值的百分比(value = self.start) / self.end)并乘以80度(因为我的车速表从5度标记开始到85结束)并且它们减去180,这样数字就会使数字顺时针转动,而不是逆时针转动。

我对画布对象的放置也可能已关闭。我的尝试是在Canvas底部设置一个四分之一圆的车速表。但是当你使用Canvas create_arc函数时,它会绘制弧的右下角,在边界框的中心,这意味着你将它设置在右下角,我需要通过边框将宽度加倍和画布的高度。我想也许也让我失望了。

1 个答案:

答案 0 :(得分:2)

我的问题是我需要创建一个偏移值并将其添加到Canvas.coords调用中的所有四个点。