为什么我在Tkinter中的饼图没有显示? [英] Why does my pie chart in Tkinter does not show?

查看:9
本文介绍了为什么我在Tkinter中的饼图没有显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序接受输入并将其保存到CSV文件,现在最后一个基本部分是显示列内容的饼图。输入内容应该是关于书籍的。因此,例如,饼图应该显示列表中所有图书的流派。

我在创建饼图方面没有问题,我在一个单独的脚本中管理它,我也没有收到错误消息,而是只有一个白色字段。

import csv
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import ttk
from collections import Counter

class FrontFrames:

    def __init__(self, master):
        super().__init__()
    #Split the screen horizontally in two parts
        top_frame = tk.Frame(master, bg="green")
        bottom_frame = tk.Frame(master, bg="yellow")
        top_frame.pack(side="top", fill="both", expand=True)
        bottom_frame.pack(side="top", fill="both", expand=True)

    #Creating the three top widgets
        self.tree = Label(top_frame)

        column = ("Title", "author", "year", "others")

        self.treeview = ttk.Treeview(top_frame, columns=column, show='headings')
        self.treeview.heading("Title", text="Title")
        self.treeview.heading("author", text="Author")
        self.treeview.heading("year", text="Year")
        self.treeview.heading("others", text="Others")
        content = "./Note.csv"
        with open(content, 'r') as file:
            csv_reader = csv.DictReader(file, skipinitialspace=True)
            for row in csv_reader:
                self.treeview.insert('', '0', values=(f'{row["title"]}',
                                                      f'{row["author"]}',
                                                      f'{row["year"]}',
                                                      f'{row["others"]}'))
                c = Counter(row["others"])

        header = list(c.keys())
        values = list(c.values())
        colors = ['r', 'g', 'b', 'c', 'y']
        f = plt.Figure(figsize=(4,4))
        #a = f.add_subplot(111)
        pie = plt.pie(values, labels=header, colors=colors, startangle=90, 
                      autopct='%.1f%%')
        self.canvas = FigureCanvasTkAgg(f, top_frame)

        self.tree.pack(side="left", fill="both", expand=True)
        self.treeview.pack(side="left", fill="both", expand=True)
        self.canvas.get_tk_widget().pack(side="right", fill="both")
        #self.canvas.show()

我注释掉了行"self.canvas.show()",因为否则我会收到错误消息

AttributeError:‘FigureCanvasTkAgg’对象没有属性‘show’

总是被告知必须包含该命令才能实际显示饼图,但在没有构建其他小部件的单独测试中,饼图是可见的。我假设饼图只是隐藏在另一个小部件下面。

CSV的内容如下所示:

title, author, year, others, note
Everything,Nobody,2222,Fiction,This is just something of everything!
Nothing,Everybody,1111,Romance,This is nothing of anything!
Pokemon,Pikachu,1999,Fiction,Once upon time.
Lord of the rings, R.R. Jackson, 1972, Fantasy, A long story!
Harry Potter, Rowling, 1995, Fantasy, A detailed story.
Bible, Apostels, 0, Fiction, A story.
The Ring, Frank, 1980, Horror, A scary story.
1984, Orwell, 1932, Dystopia, A scary future for the past and scary present 
for us.
Pirates of the Caribbean, Brandow, 1955, Fiction, A pirate story.

我使用的是Python3.7.0

一如既往,非常感谢您的有用答案。

推荐答案

对于self.canvas.show,我的Python解释器告诉我FigureCanvasTkAgg.show已弃用,我应该改用draw

关于绘图的问题,我设法通过取消注释a = f.add_subplot(111)并替换

来显示饼图
plt.pie(values, labels=header, colors=colors, startangle=90, autopct='%.1f%%')

a.pie(values, labels=header, colors=colors, startangle=90, autopct='%.1f%%') 

所以我猜您的图表显示在错误的图形中,因为您没有显式指定轴。

这篇关于为什么我在Tkinter中的饼图没有显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆