将Python熊猫图嵌入到Tkinter GUI画布小部件'update_idletasks'问题

时间:2018-11-15 11:35:40

标签: python pandas tkinter

我正试图将熊猫情节嵌入Tkinter。 我可以显示它;但是,当程序第一次运行并关闭时,我收到AttributeError:'NoneType'对象没有属性'update_idletasks'错误。 这是我的代码:

const head = block.gallery.shift(); // removes and stores first element in "head"

console.log(block.gallery) // array with first element removed

即使我传递了Figure对象,为什么仍会收到该错误?

最好的问候

解决方案:

在python中,当您执行a()。b()时,表达式的结果就是b()返回的值,因此LabelFrame(...)。grid(...)将返回None。

因此解决方案将LabelFrame初始化更改为以下内容:

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import pandas as pd
import tkinter as tk
import os
matplotlib.use("TkAgg")


LOCO_ANCHOR_DATA = "data_loco/loco_anchor"
POS_DATA = "data_loco/pos_data"


class ReportWindow:
    def __init__(self, master):
        self.master = master

        tk.Label(self.master, text="Report").grid(row=0, column=0)
        self.lf = tk.LabelFrame(self.master, text="Plot").grid(row=1, column=0, padx=3, pady=3)
        self.draw_plot()

    def draw_plot(self):
        fig = self.plot_data()

        canvas = FigureCanvasTkAgg(fig, master=self.lf)
        canvas.get_tk_widget().grid(row=1, column=0, padx=5, pady=5)

    def read_anchor_file(self, data_path=LOCO_ANCHOR_DATA):
        anchor_csv_path = os.path.join(data_path, "anchor_pos.csv")
        # print(pd.read_csv(csv_path))
        return pd.read_csv(anchor_csv_path)

    def read_pos_file(self, data_path=POS_DATA):
        pos_csv_path = os.path.join(data_path, "real_time_data.csv")
        # print(pd.read_csv(pos_csv_path))
        return pd.read_csv(pos_csv_path)

    def plot_data(self):
        fig = Figure(figsize=(5,4), dpi=100)
        plt.rc('grid', linestyle="--", color='black')
        axis = fig.add_subplot(111)
        anchor_pos_data = self.read_anchor_file()
        drone_pos_data = self.read_pos_file()
        drone_pos_data.plot(kind="scatter", x="xpos", y="ypos", alpha=0.4,
                            s=50, label="Real Time Position",
                            c="zpos", cmap=plt.get_cmap("jet"), colorbar=True, ax=axis)
        anchor_pos_data.plot(kind="scatter", x="y_direction", y="x_direction",
                             label="Anchor Positions",
                             color=(0, 0, 0), colorbar=False, ax=axis)
        plt.colormaps()
        plt.title("Flight Path Map")
        plt.grid(True)
        plt.legend(loc=0)
        # plt.show()
        return fig


if __name__ == '__main__':
    root = tk.Tk()
    report_gui = ReportWindow(master=root)
    root.mainloop()

然后我不再看到“ update_idletasks”问题

0 个答案:

没有答案