简体   繁体   English

在tkinter中积分图给出更新idletasks错误

[英]Integrating plot in tkinter giving update idletasks error

I'm trying to create a super simple GUI and integrate a plot on it. 我正在尝试创建一个超级简单的GUI并在其上集成绘图。 I managed to integrate the plot but I have the Error: 我设法整合了情节,但出现错误:

AttributeError: 'NoneType' object has no attribute 'update_idletasks'

This question refers to the problem, but the answer doesn't really solve the problem of the update idletasks 这个问题是关于问题的,但是答案并没有真正解决更新闲置任务的问题。

My code is super simple, just two labels in a grid and I want t plot in the third row. 我的代码非常简单,网格中只有两个标签,我不想在第三行中绘图。

from tkinter import Label, Tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg


class MyGUI:
    def __init__(self):
        self.window = Tk()
        self.window.geometry('420x400')
        self.populate_elements()
        self.window.mainloop()

    def populate_elements(self):
        Label(self.window, text="Hello").grid(column=0, row=0)
        Label(self.window, text="My text").grid(column=0, row=1)

        fig = plt.Figure(figsize=(2, 2), dpi=100)
        ax = fig.add_subplot(111)
        ax.plot([1, 2, 3, 4, 5, 6, 7, 8], [5, 6, 1, 3, 8, 9, 3, 5])

        canvas = FigureCanvasTkAgg(fig)
        canvas.get_tk_widget().grid(column=0, row=2)


if __name__ == '__main__':
    MyGUI()

Matplotlib has an example about embedding in tk: https://matplotlib.org/3.1.1/gallery/user_interfaces/embedding_in_tk_sgskip.html Matplotlib有一个有关在tk中嵌入的示例: https ://matplotlib.org/3.1.1/gallery/user_interfaces/embedding_in_tk_sgskip.html

If you stick to it, you will find that you need to use 如果坚持下去,您会发现需要使用

canvas = FigureCanvasTkAgg(fig, master=self.window)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM