Python 用海龟或特金特制作蜘蛛图
最近,我在大学为我的辅修课程学习了两门python编码的初级课程,之后是数据库简介和PHP中CRUD开发的基础课程。python课程相当不错,涉及了大部分基础知识。我一直在尝试通过做一些小应用来自学,这些应用是我在语言学剩余课程中必须做的事情 现在我想编写一个小型python应用程序,它需要用户输入来为说话者创建一个语言配置文件。我想我可以使用python的turtle模块来实现这一点,但是在绘制网格之后,我发现我不知道如何获得我绘制的圆的点 我编写了以下代码:Python 用海龟或特金特制作蜘蛛图,python,python-3.x,tkinter,geometry,tkinter-canvas,Python,Python 3.x,Tkinter,Geometry,Tkinter Canvas,最近,我在大学为我的辅修课程学习了两门python编码的初级课程,之后是数据库简介和PHP中CRUD开发的基础课程。python课程相当不错,涉及了大部分基础知识。我一直在尝试通过做一些小应用来自学,这些应用是我在语言学剩余课程中必须做的事情 现在我想编写一个小型python应用程序,它需要用户输入来为说话者创建一个语言配置文件。我想我可以使用python的turtle模块来实现这一点,但是在绘制网格之后,我发现我不知道如何获得我绘制的圆的点 我编写了以下代码: ''' for i in ra
'''
for i in range(6):
t.circle(20*i,None, 16)
t.seth(90)
t.penup()[enter image description here][1]
t.backward(20)
t.pendown()
t.seth(0)
'''
我用了16个步骤。因为它最终需要看起来像这张图片:
这里有谁能帮我把圆圈上的这些点绘制成地图,这样在用户告诉程序他对该点的熟练程度后就可以画出来
如果我遗漏了什么重要的东西,请告诉我
诚恳地说,你可以用
tkinter
构建雷达或蜘蛛图
下面的类获取一个元组列表(“标签”,分数)
,分数表示为百分比(区间值[01100]
)
数据点的数量根据提供的数据进行调整。图表的比例可以调整
import math
import tkinter as tk
class SpiderChart(tk.Canvas):
"""a canvas that displays datapoints as a SpiderChart
"""
width=500
height=500
def __init__(self, master, datapoints, concentrics=10, scale=200):
super().__init__(master, width=self.width, height=self.height)
self.scale = scale
self.center = self.width // 2, self.height // 2
self.labels = tuple(d[0] for d in datapoints)
self.values = tuple(d[1] for d in datapoints)
self.num_pts = len(self.labels)
self.concentrics = [n/(concentrics) for n in range(1, concentrics + 1)]
self.draw()
def position(self, x, y):
"""use +Y pointing up, and origin at center
"""
cx, cy = self.center
return x + cx, cy - y
def draw_circle_from_radius_center(self, radius):
rad = radius * self.scale
x0, y0 = self.position(-rad, rad)
x1, y1 = self.position(rad, -rad)
return self.create_oval(x0, y0, x1, y1, dash=(1, 3))
def draw_label(self, idx, label):
angle = idx * (2 * math.pi) / self.num_pts
d = self.concentrics[-1] * self.scale
x, y = d * math.cos(angle), d * math.sin(angle)
self.create_line(*self.center, *self.position(x, y), dash=(1, 3))
d *= 1.1
x, y = d * math.cos(angle), d * math.sin(angle)
self.create_text(*self.position(x, y), text=label)
def draw_polygon(self):
points = []
for idx, val in enumerate(self.values):
d = (val / 100) * self.scale
angle = idx * (2 * math.pi) / self.num_pts
x, y = d * math.cos(angle), d * math.sin(angle)
points.append(self.position(x, y))
self.create_polygon(points, fill='cyan')
def draw(self):
self.draw_polygon()
for concentric in self.concentrics:
self.draw_circle_from_radius_center(concentric)
for idx, label in enumerate(self.labels):
self.draw_label(idx, label)
data = [('stamina', 70), ('python-skill', 100), ('strength', 80), ('break-dance', 66), ('speed', 45), ('health', 72), ('healing', 90), ('energy', 12), ('libido', 100)]
root = tk.Tk()
spider = SpiderChart(root, data)
spider.pack(expand=True, fill=tk.BOTH)
root.mainloop()