首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用海龟或tkinter构建一个蜘蛛图

使用海龟或tkinter构建一个蜘蛛图
EN

Stack Overflow用户
提问于 2020-11-17 01:01:04
回答 1查看 116关注 0票数 0

我最近在大学辅修了两门python编码入门课程,随后是数据库入门和PHP CRUD开发的基础课程。python课程相当不错,涉及到了大多数基础知识。我一直在试图通过制作小应用程序来自学,以完成我在剩余的语言学课程中必须做的事情。

现在,我想编写一个小的python应用程序,它接受用户输入来为该演讲者创建语言配置文件。我认为我可以使用python的turtle模块来做这件事,但是在绘制网格之后,我发现我不知道如何获得我所绘制的圆的点。

我写了下面这段代码:

代码语言:javascript
复制
'''
 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步。因为它最终需要看起来像这样的图片:

在座的各位能不能告诉我,如何绘制圆上的这些点,以便在用户告诉程序他对这一点的熟练程度后,可以将它们绘制出来?

如果我遗漏了什么重要的东西,请告诉我。

由衷地,

EN

回答 1

Stack Overflow用户

发布于 2020-11-17 12:01:30

您可以使用tkinter构建雷达或蜘蛛图。

下面的类获取一个间隔列表,分数以百分比( tuples('label', score) [0, 100]中的值)表示。

数据点的数量将根据提供的数据进行调整。

图表的比例可以调整。

代码语言:javascript
复制
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()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64862345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档