I have an app created by using tkinter. There is just a window in which I'm typing X and Y. Having these x and y, my app should create a graph using matplotlib.
First time it works correctly, but others no. I mean, I open the app, input X and Y, click button and the app creates graph, then I input new X and Y, click button and this doesn't work, graph is the same, not new.
I've searched many sites, but no one of solutions works.
a part of code looks like:
from tkinter import *
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
window_width = 1000
window_height = 600
root = Tk()
global_fig = None
def create_draw(x_dots, y_dots):
global global_fig
if global_fig is not None:
global_fig.clf()
global_fig = Figure(figsize=(5, 4), dpi=100)
global_fig.add_subplot(111).plot(x_dots, y_dots)
canvas = FigureCanvasTkAgg(global_fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
root.update()
global_fig.canvas.draw()
root.mainloop()
the sense of problem is:
root.mainloop()
create_draw([3,4], [2,3])
after typing it I see a graph, then I type
create_draw([-1, -2], [-3, -4])
And I should see another graph, but I see old graph in tkinter.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…