首页 文章

Matplotlib图在tkinter GUI中停止更新

提问于
浏览
1

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

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

Image of the Test GUI

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

1 回答

  • 0

    一切正常,即正在绘制线条,但需要更新图形 . 您可以在绘图后使用 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')
    

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

相关问题