3

I am using tkinter in python. I would like to be able to plot data as I click on the button compute. and also I would like also to take into account the values given in the entries. Up to now my script run normally, but nothing happen when I click compute of change the entry values. here is the code :

#------- 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()
asked Jun 13, 2020 at 13:01

2 Answers 2

2

You were missing a small detail in your MyWindow.plot() method, and it is to actually draw your plots. If you modify it like this (note the additional line in the function):

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

Then you get the expected behavior (for the default case in this example):

enter image description here

answered Jun 13, 2020 at 13:45
2
  • Thank you that works pretty find ^^ ! How can I clear the plots before adding a new one ? Commented Jun 13, 2020 at 15:13
  • 1
    @user3733333 no problem. You can get more information about clearing the plots in this post. A quick and dirty way of doing it might be to use draw again with empty lists. Commented Jun 13, 2020 at 15:54
1

In order to clear the plots before adding a new one, you can use :

your_fig.clf() (clears the figure)

or

your_fig.cla() (clears the axis).

or

ax.clear()

:)

answered Apr 23, 2021 at 7:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.