简体   繁体   English

Matplotlib图在Tkinter GUI中停止更新

[英]Matplotlib plot stopped updating in tkinter GUI

I'm working on this application which involves plotting a line or circle on an empty plot after pressing a button. 我正在开发此应用程序,其中涉及在按下按钮后在空图上绘制直线或圆。 It was all well until suddenly, the graph just stopped updating. 一切都很好,直到突然之间,图表才停止更新。 For example: 例如:

import math
import tkinter as tk
from tkinter import *

import matplotlib
import matplotlib.pyplot as plt
from numpy import arange, sin, cos, tan, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from matplotlib.patches import Circle
# Initialising the graph
f = Figure(figsize=(6, 6), dpi=100)
a = f.add_subplot(111)

root = tk.Tk()

class Application(tk.Frame):
  def __init__(self, master=None):
    super().__init__(master)
    self.pack()
    self.mainwindow()

  def mainwindow(self):
    self.add_loci = Button(self)
    self.add_loci["text"] = "Add Line"
    self.add_loci["command"] = self.line
    self.add_loci.pack(side = TOP)

    # Plotting an empty graph
    a.set_xlabel('Re')
    a.set_ylabel('Im')
    a.grid(b=None)
    a.plot(0, 0)
    # Setting up and showing the toolbar and the graph
    canvas = FigureCanvasTkAgg(f, master=root)
    canvas.show()
    canvas.get_tk_widget().pack(side = BOTTOM, fill=BOTH, expand=1)
    toolbar = NavigationToolbar2TkAgg(canvas, root)
    toolbar.update()
    canvas._tkcanvas.pack(side = BOTTOM, fill=BOTH, expand=1)

  def line(self):
    a.plot([0, 10], [0, 10])
    print('test')

app = Application(master=root)
app.mainloop()

测试GUI的图像

The supposed outcome when pressing the button is that a straight should appear, but it doesn't. 按下按钮时的预期结果是应该出现一条直线,但事实并非如此。 I added the print statement to check if the button is working correctly and it does, so I really don't know what the problem here is. 我添加了print语句,以检查按钮是否正常工作并且可以正常工作,所以我真的不知道这里出了什么问题。 I'm currently using Spyder (3.2.4). 我目前正在使用Spyder(3.2.4)。

Everything is working, ie the line is being plotted, however the figure needs to be updated. 一切正常,即正在绘制线,但是该图需要更新。 You can do this using figure.canvas.draw() after your plot. 您可以在绘图后使用figure.canvas.draw()进行此操作。 Your code would look something like: 您的代码如下所示:

f = Figure(figsize=(6, 6), dpi=100)
a = f.add_subplot(111)

root = tk.Tk()

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.mainwindow()

    def mainwindow(self):
        self.add_loci = Button(self)
        self.add_loci["text"] = "Add Line"
        self.add_loci["command"] = self.line
        self.add_loci.pack(side = TOP)

        # Plotting an empty graph
        a.set_xlabel('Re')
        a.set_ylabel('Im')
        a.grid(b=None)
        a.plot(0, 0)
        # Setting up and showing the toolbar and the graph
        canvas = FigureCanvasTkAgg(f, master=root)
        canvas.show()
        canvas.get_tk_widget().pack(side = BOTTOM, fill=BOTH, expand=1)
        toolbar = NavigationToolbar2TkAgg(canvas, root)
        toolbar.update()
        canvas._tkcanvas.pack(side = BOTTOM, fill=BOTH, expand=1)

    def line(self):

        a.plot([0, 10], [0, 10])
        f.canvas.draw()

        print('test')

app = Application(master=root)
app.mainloop()

You could also make the canvas you create an instance variable, by using self.canvas = ... . 您还可以通过使用self.canvas = ...使画布创建实例变量。 Then using self.canvas.draw() . 然后使用self.canvas.draw() Your mainwindow and line function would then look like: 您的mainwindowline函数将如下所示:

def mainwindow(self):
    self.add_loci = Button(self)
    self.add_loci["text"] = "Add Line"
    self.add_loci["command"] = self.line
    self.add_loci.pack(side = TOP)

    # Plotting an empty graph
    a.set_xlabel('Re')
    a.set_ylabel('Im')
    a.grid(b=None)
    a.plot(0, 0)
    # Setting up and showing the toolbar and the graph
    self.canvas = FigureCanvasTkAgg(f, master=root)
    self.canvas.show()
    self.canvas.get_tk_widget().pack(side = BOTTOM, fill=BOTH, expand=1)
    toolbar = NavigationToolbar2TkAgg(self.canvas, root)
    toolbar.update()
    self.canvas._tkcanvas.pack(side = BOTTOM, fill=BOTH, expand=1)

def line(self):

    a.plot([0, 10], [0, 10])
    self.canvas.draw()

    print('test')

Both would produce the same result. 两者都会产生相同的结果。

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

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