【问题标题】:Matplotlib plot stopped updating in tkinter GUIMatplotlib 图在 tkinter GUI 中停止更新
【发布时间】:2018-03-26 04:42:50
【问题描述】:

我正在开发这个应用程序,它涉及在按下按钮后在空白图上绘制一条线或圆。一切都很好,直到突然,图表停止更新。例如:

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()

按下按钮时的假设结果是应该出现顺子,但事实并非如此。我添加了打印语句来检查按钮是否正常工作并且确实如此,所以我真的不知道这里的问题是什么。我目前正在使用 Spyder (3.2.4)。

【问题讨论】:

    标签: python python-3.x matplotlib tkinter


    【解决方案1】:

    一切正常,即正在绘制线条,但是需要更新图形。您可以在绘图后使用figure.canvas.draw() 执行此操作。您的代码如下所示:

    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()
    

    您还可以使用self.canvas = ... 使您创建实例变量的画布。然后使用self.canvas.draw()。您的 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')
    

    两者都会产生相同的结果。

    【讨论】:

      猜你喜欢
      • 2015-08-26
      • 2013-11-05
      • 2022-08-13
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      相关资源
      最近更新 更多