【问题标题】:Building a spider-chart with turtle or tkinter使用 turtle 或 tkinter 构建蜘蛛图
【发布时间】:2020-11-16 17:01:04
【问题描述】:

我最近在大学为我的 Minor 开设了 2 门 Python 编码初学者课程,随后是数据库介绍和 PHP 中 CRUD 开发的基础课程。 python 课程足够好,并且触及了大部分基础知识。我一直在尝试通过制作小型应用程序来完成我在语言学剩余课程中必须做的事情来自学。

现在我想编写一个小型 python 应用程序,它需要用户输入来为该扬声器创建一个语言配置文件。我以为我可以使用python的turtle模块来做到这一点,但是在绘制网格后我发现我不知道如何获取我绘制的圆圈的点。

我写了以下代码:

'''
 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 个步骤。因为它最终需要看起来像这张图片:

这里的任何人都可以帮助我了解如何在圆圈上绘制这些点,以便在用户告诉程序他对该点的熟练程度之后将它们绘制出来吗?

如果我遗漏了任何重要的内容,请告诉我。

此致,

【问题讨论】:

    标签: python python-3.x tkinter geometry tkinter-canvas


    【解决方案1】:

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

    以下类采用tuples('label', score) 的列表,分数以百分比表示(区间[0, 100] 中的值)。

    数据点的数量会根据提供的数据进行调整。
    图表的比例可以调整。

    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()
            
    

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多