Python Forum
Update plot by <Return> bind with entry widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update plot by <Return> bind with entry widget
#1
Hey guys.. I need short hint:

I want to update my matplotlib plot after the user enters a number in Entry widget and hint <Return>.
I can create a default plot with default values, but somehow the graph does not update after changing the number and hitting <Return> again.

To check, if the bind works, I've created the printEntry() method. It will be called after calling the method to plot the graph. And this works!

Any suggestions ???
Thx you in advance.....


Here my code:



try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import numpy as np
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top

def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = Toplevel1 (root)
    init(root, top)
    root.mainloop()


class Toplevel1:
    def __init__(self, top=None):

        self.style = ttk.Style()
        self.style.configure('.',font="TkDefaultFont")

        top.geometry("826x862+639+123")
        top.title("interactive plot")

        self.TLabel1 = ttk.Label(top)
        self.TLabel1.place(relx=0.024, rely=0.023, height=24, width=83)
        self.TLabel1.configure(relief='flat')
        self.TLabel1.configure(text='''Input value''')
        
        ## Frame 1 #############################################
        self.Frame1 = tk.Frame(top)
        self.Frame1.place(relx=0.012, rely=0.058, relheight=0.102
                , relwidth=0.975)
        self.Frame1.configure(relief='groove')
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(highlightcolor="black")
        self.Frame1.configure(width=805)
        
        self.Entry1_1 = tk.Entry(self.Frame1)
        self.Entry1_1.place(relx=0.025, rely=0.114,height=35, relwidth=0.124)
        self.Entry1_1.configure(highlightcolor="black")
        self.Entry1_1.configure(selectforeground="black")
        self.Entry1_1.insert(0,1)
        self.Entry1_1.bind("<Return>", self.matplotCanvas)


        ##########################################################
        ## Frame 2 #############################################
        self.TLabel1_11 = ttk.Label(top)
        self.TLabel1_11.place(relx=0.024, rely=0.174, height=24, width=83)
        self.TLabel1_11.configure(relief='flat')
        self.TLabel1_11.configure(text='''Plot''')

        self.Frame2 = tk.Frame(top)
        self.Frame2.place(relx=0.012, rely=0.209, relheight=0.58, relwidth=0.969)
        self.Frame2.configure(relief='groove')
        self.Frame2.configure(borderwidth="2")
        self.Frame2.configure(relief='groove')
        self.Frame2.configure(highlightcolor="black")
        self.Frame2.configure(width=805)
        ###########################################################
        
        self.TButton_close = ttk.Button(top)
        self.TButton_close.place(relx=0.86, rely=0.951, height=25, width=96)
        self.TButton_close.configure(command=destroy_window)
        self.TButton_close.configure(takefocus="")
        self.TButton_close.configure(text='''Close''')

        self.matplotCanvas()
        
        
    
    def matplotCanvas(self, *args):
        x = np.linspace(0,0.05,num=500)
        y = float(self.Entry1_1.get())*x**2
        self.printEntry()
        f = Figure(figsize = (5,5), dpi=100)
        a = f.add_subplot(111)
        a.clear()
        a.plot(x,y)
        a.grid(True)
        canvas = FigureCanvasTkAgg(f, self.Frame2)
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        
    
    def printEntry(self, *args):
        print(float(self.Entry1_1.get()))


    
if __name__ == '__main__':
    vp_start_gui()
Reply
#2
So I got it....
Check it out and let me know if you have some suggestions for improvement.

Cheers...

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top

def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None

class App:
    def __init__(self, master):
        
        global top_level
        top_level = root
        
        root.geometry("826x862+639+123")
        root.title("interactive plot")
        
        self.TLabel1 = ttk.Label(root)
        self.TLabel1.place(relx=0.024, rely=0.023, height=24, width=83)
        self.TLabel1.configure(relief='flat')
        self.TLabel1.configure(text='''Input value''')
        ##########################################################
        ## Frame 1 ###############################################
        self.Frame1 = tk.Frame(root)
        self.Frame1.place(relx=0.012, rely=0.058, relheight=0.102
                , relwidth=0.975)
        self.Frame1.configure(relief='groove')
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(highlightcolor="black")
        self.Frame1.configure(width=805)
        
        self.Entry1_1 = tk.Entry(self.Frame1)
        self.Entry1_1.place(relx=0.025, rely=0.114,height=35, relwidth=0.124)
        self.Entry1_1.configure(highlightcolor="black")
        self.Entry1_1.configure(selectforeground="black")
        self.Entry1_1.insert(0,1)
        self.Entry1_1.bind("<Return>", self.updatePlot)
        ##########################################################
        ## Frame 2 ###############################################
        self.TLabel1_11 = ttk.Label(root)
        self.TLabel1_11.place(relx=0.024, rely=0.174, height=24, width=83)
        self.TLabel1_11.configure(relief='flat')
        self.TLabel1_11.configure(text='''Plot''')

        self.Frame2 = tk.Frame(root)
        self.Frame2.place(relx=0.012, rely=0.209, relheight=0.58, relwidth=0.969)
        self.Frame2.configure(relief='groove')
        self.Frame2.configure(borderwidth="2")
        self.Frame2.configure(relief='groove')
        self.Frame2.configure(highlightcolor="black")
        self.Frame2.configure(width=805)
        
        ##########################################################
        ## create plot with default values from Entry widget
        fig = Figure()
        self.subplot1 = fig.add_subplot(111)
        x = np.linspace(0,0.05,500)
        meanVal = float(self.Entry1_1.get())
        y = meanVal*np.sqrt(2)*np.sin(2*np.pi*50*x)
        self.line, = self.subplot1.plot(x,y)
        self.subplot1.grid(True)
        self.subplot1.autoscale(enable=True)
                
        self.canvas = FigureCanvasTkAgg(fig, master=self.Frame2)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        ##########################################################
        ## Buttons ###############################################
        
        self.TButton_close = ttk.Button(root)
        self.TButton_close.place(relx=0.86, rely=0.951, height=25, width=96)
        self.TButton_close.configure(command=destroy_window)
        self.TButton_close.configure(takefocus="")
        self.TButton_close.configure(text='''Close''')
        
    def updatePlot(self, *args):
        x, y = self.line.get_data()
        meanVal = float(self.Entry1_1.get())
        self.subplot1.cla()
        y = meanVal*np.sqrt(2)*np.sin(2*np.pi*50*x)**meanVal
        self.subplot1.plot(x,y)
        self.subplot1.grid(True)
        self.canvas.draw()
    
if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    init(root, app)
    root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 1,685 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  method to add entries in multi columns entry frames in self widget sudeshna24 2 576 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 2,867 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 2,472 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
  [Tkinter] password with Entry widget TAREKYANGUI 9 1,863 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 1,804 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 1,256 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  [Tkinter] Update matplotlib plot correctly Particledust 0 2,326 Apr-20-2020, 04:59 PM
Last Post: Particledust
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 3,010 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  [WxPython] Bind error PeterLinux 1 973 Apr-06-2020, 03:07 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020