2

我正在构建一个应用程序,它接受输入,将其保存到 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.

我使用 Python 3.7.0

与往常一样,非常感谢您提供的有用答案。

4

1 回答 1

4

至于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%%') 

所以我的猜测是你的图表显示在错误的图中,因为你没有明确指定轴。

于 2018-10-02T13:32:30.913 回答