通过单击按钮tkinter python更新图

时间:2020-06-13 13:01:57

标签: python matplotlib tkinter

我在python中使用tkinter。我希望能够在单击按钮计算时绘制数据。并且我也想考虑条目中给出的值。 到目前为止,我的脚本可以正常运行,但是当我单击更改条目值的计算时没有任何反应。 这是代码:

#------- start
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import numpy as np

class MyWindow:
    def __init__(self, win):
        x0, xt0, y0 = 10, 100, 50
        #---- First label and entry -------
        self.lbl0 = Label(win, text='Initial instant')
        self.lbl0.config(font=('Arial', 9))
        self.lbl0.place(x=x0, y=y0)
        self.t0 = Entry()
        self.t0.place(x=xt0, y=y0)
        self.t0.insert(END, str(0))
        self.t_0 = float(self.t0.get())

        #---- Second label and entry -------
        self.lbl1 = Label(win, text='Final instant')
        self.lbl1.config(font=('Arial', 10))
        self.lbl1.place(x=x0, y=y0 + 40)
        self.t1 = Entry()
        self.t1.place(x=xt0, y=y0 + 40)
        self.t1.insert(END, str(1))
        self.t_1 = float(self.t1.get())

        #---- Compute button -------
        self.btn = Button(win, text='Compute')
        self.btn.bind('<Button-1>', self.plot)
        self.btn.place(x=xt0, y=y0 + 80)

        self.figure = Figure(figsize=(4.5, 3), dpi=100)

        #---- subplot 1 -------
        self.subplot1 = self.figure.add_subplot(211)
        self.subplot1.set_xlim(self.t_0, self.t_1)

        #---- subplot 2 -------
        self.subplot2 = self.figure.add_subplot(212)
        self.subplot2.set_xlabel('$Time(s)$', fontsize=11)
        self.subplot2.set_xlim(self.t_0, self.t_1)

        #---- Show the plot-------
        self.plots = FigureCanvasTkAgg(self.figure, win)
        self.plots.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=0)

    def data(self):
        self.t_0 = float(self.t0.get())
        self.t_1 = float(self.t1.get())
        t = np.linspace(self.t_0, self.t_1, 100)
        func1 = t**0.5
        func2 = t**2.
        return t, func1, func2

    def plot(self, event):
        t, func1, func2 = self.data()
        self.t_0 = float(self.t0.get())
        self.t_1 = float(self.t1.get())
        self.subplot1.set_xlim(self.t_0, self.t_1)
        self.subplot1.plot(t, func1, 'r', lw=2.5)
        self.subplot2.set_xlim(self.t_0, self.t_1)
        self.subplot2.plot(t, func2, 'b', lw=2.5)


window = Tk()
mywin = MyWindow(window)
window.title('My model')
window.geometry("800x600+10+10")
window.mainloop()

2 个答案:

答案 0 :(得分:2)

您在MyWindow.plot()方法中缺少一个小细节,它实际上是绘制您的绘图。如果您这样修改它(请注意函数中的其他行):

def plot(self, event):
    t, func1, func2 = self.data()
    self.t_0 = float(self.t0.get())
    self.t_1 = float(self.t1.get())
    self.subplot1.set_xlim(self.t_0, self.t_1)
    self.subplot1.plot(t, func1, 'r', lw=2.5)
    self.subplot2.set_xlim(self.t_0, self.t_1)
    self.subplot2.plot(t, func2, 'b', lw=2.5)
    self.plots.draw()

然后,您将获得预期的行为(在此示例中为默认情况):

enter image description here

答案 1 :(得分:0)

为了在添加新图之前清除图,您可以使用:

your_fig.clf()(清除图形)

your_fig.cla()(清除轴)。

ax.clear()

:)