【问题标题】:update plot by a button click on tkinter python通过单击 tkinter python 按钮更新绘图
【发布时间】:2020-10-03 04:57:00
【问题描述】:

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

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

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

    然后你会得到预期的行为(对于本例中的默认情况):

    【讨论】:

    • 谢谢你的作品很好找^^!如何在添加新地块之前清除地块?
    • @user3733333 没问题。您可以在此post 中获取有关清除绘图的更多信息。一种快速而肮脏的方法可能是再次使用 draw 和空列表。
    【解决方案2】:

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

    your_fig.clf()(清零)

    your_fig.cla()(清除轴)。

    ax.clear()
    

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-23
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      相关资源
      最近更新 更多