I wanna update a embedded chart in a Tkinter GUI with a button and I am not really sure how to do this, right now the closest solution to this that i found is this:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tkinter import *
# Creation of the mainwindow and container frame
root = Tk()
root.state('zoomed')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
mainframe = Frame(root)
mainframe.grid(row=0, column=0, sticky='nsew')
mainframe.tkraise()
# Embed the chart to the Tkinter Window
fig1 = plt.Figure(figsize=(8, 3), dpi=100)
plot1 = fig1.add_subplot(111)
chart1 = FigureCanvasTkAgg(fig1, mainframe)
chart1.get_tk_widget().place(x=300, y=10)
# Button section
def plot_graph():
A = [2, 5, 7, 4, 2, 0, 1, 2, 3]
fig1 = plt.Figure(figsize=(8, 3), dpi=100)
plot1 = fig1.add_subplot(111).plot(A)
chart1 = FigureCanvasTkAgg(fig1, mainframe)
chart1.get_tk_widget().place(x=300, y=10)
plot_btn = Button(mainframe, text='Plot graph', command=plot_graph)
plot_btn.grid(row=0,column=0)
# Mainloop
root.mainloop()
The problem with this is that when i press the plot button a new graph is created and inserted over the previous one and i dont want to do that, i just want to update the first one that i created. I am pretty new with Tkinter, any idea of how i can do this?
Related
I was wondering how you can completely delete a plot from a tkinter window.
Assuming i would have a tkinter project like the following:
import tkinter as tk
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
data1 = {'Country': ['US','CA','GER','UK','FR'],
'GDP_Per_Capita': [45000,42000,52000,49000,47000]
}
df1 = DataFrame(data1,columns=['Country','GDP_Per_Capita'])
root= tk.Tk()
figure1 = plt.Figure(figsize=(6,5), dpi=100)
ax1 = figure1.add_subplot(111)
bar1 = FigureCanvasTkAgg(figure1, root)
bar1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
df1 = df1[['Country','GDP_Per_Capita']].groupby('Country').sum()
df1.plot(kind='bar', legend=True, ax=ax1)
ax1.set_title('Country Vs. GDP Per Capita')
def close_plot():
plt.close(figure1)
button_delete = Button(root, text='Delete', command = lambda: close_plot()).place(height=30, width = 100, rely=0.02, relx = 0.4)
root.mainloop()
I veen trying to use matplotlib.pyplot.close within a button but it doens't seem to work.
Anyone have a clue how to get rid of this plot.
Thank you very much!
Your figure is embedded in a tkinter widget. You need to keep a reference to that widget, and use its methods to add/remove it from the tkinter window:
Here we use pack_forget(): the widget is removed from the window, but still exists, and could be reused later.
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def remove_plot():
w.pack_forget() # here you remove the widget from the tk window
# w.destroy()
if __name__ == '__main__':
# data
x, y = [1, 2, 3, 4], [1, 4, 9, 16]
# matplotlib stuff
figure1 = plt.Figure(figsize=(6, 5), dpi=100)
ax1 = figure1.add_subplot(111)
ax1.plot(x, y)
ax1.set_title('Country Vs. GDP Per Capita')
# tkinter stuff
root = tk.Tk()
bar1 = FigureCanvasTkAgg(figure1, root)
w = bar1.get_tk_widget()
w.pack(side=tk.LEFT, fill=tk.BOTH) # here you insert the widget in the tk window
button_delete = tk.Button(root, text='Remove Plot', command=remove_plot)
button_delete.place(height=30, width=100, rely=0.02, relx=0.4) # place is an odd choice of geometry manager, you will have to adjust it every time the title changes
root.mainloop()
Alternatively, if you don't need that figure any longer, you could use w.destroy (commented line) to destroy the widget.
I would like to change the color of the toolbar when making a matplotlib figure in tkinter. I have managed to find and change the color of two parts. There is one remaining.
My code comes directly from https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_tk_sgskip.html?highlight=embedding%20tk with three additional lines to change colors.
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import numpy as np
root = tkinter.Tk()
root.wm_title("Embedding in Tk")
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot().plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
color = "#d469a3"
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.config(background=color)
toolbar._message_label.config(background=color)
toolbar.update()
button = tkinter.Button(master=root, text="Quit", command=root.quit)
button.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
This gives me the window:
What is the small, grey rectangle I have pointed out? How do I change its color?
It is an empty label. You can get a reference to it via winfo_children:
print (toolbar.winfo_children()[-2])
# .!navigationtoolbar2tk.!label
And to change its color:
toolbar.winfo_children()[-2].config(background=color)
I've got multiple pages in a notebook and have multiple graphs on each.
Is there a way to use one navigation toolbar for all graphs on a notebook page? Or how can I put each navigation toolbar directly underneath the graph it controls?
I've tried adding padx and pady which I thought would force it under the graph, but it keeps putting both toolbars underneath the Quit button.
I'm pretty new to Python and am really struggling to find an answer to this question!
import numpy as np
import matplotlib.pyplot as plt
import tkinter as app
from tkinter import ttk
from matplotlib.collections import LineCollection
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
x = np.linspace(1, 10, 10)
y = np.square(x)
z = x
#Make the root widget
root = app.Tk()
#Make the notebook
nb = ttk.Notebook(root)
nb.pack()
root.title("Test")
#plot first graph
f1 = app.Frame(nb)
nb.add(f1, text="Test")
app.Label(f1, text="Test window 1").pack(padx=5, pady=5)
fig1 = Figure(figsize=(5, 4), dpi=100)
fig1.add_subplot(111).plot(x, y)
canvas1 = FigureCanvasTkAgg(fig1, master=f1)
canvas1.draw()
canvas1.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1)
#add toolbar
toolbar1 = NavigationToolbar2Tk(canvas1, f1)
toolbar1.update()
canvas1.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1)
#plot second graph
fig2 = Figure(figsize=(5, 4), dpi=100)
fig2.add_subplot(111).plot(x, z)
canvas2 = FigureCanvasTkAgg(fig2, master=f1)
canvas2.draw()
canvas2.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1, padx=5, pady=5)
#add toolbar
toolbar2 = NavigationToolbar2Tk(canvas2, f1)
toolbar2.update()
canvas2.get_tk_widget().pack(side=app.TOP, fill=app.BOTH, expand=1)
app.Button(f1, text='Exit', command=root.destroy).pack(padx=50, pady=50)
nb.select(f1)
nb.enable_traversal()
#Enter the mainloop
root.mainloop()
I get the following error when I resize the right expand the window and then close it:
_tkinter.TclError: invalid command name ".!scrollbar"
Maybe it's related to the canvas widget.
I tried a lot, but without results.
can anyone help me?
The code is below
import tkinter as tk
from matplotlib.figure import Figure
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root=tk.Tk()
vscrollbar = tk.Scrollbar(root)
canvasF2= tk.Canvas(root,yscrollcommand=vscrollbar.set)
vscrollbar.config(command=canvasF2.yview)
vscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
frame2=tk.Frame(canvasF2) #Create the frame which will hold the widgets
canvasF2.pack(side="left", fill="both", expand=True)
##Updated the window creation
canvasF2.create_window(0,0,window=frame2, anchor='nw')
#
fig = Figure(figsize=(10, 4), dpi=100)
t = np.arange(0, 3, .01)
a = fig.add_subplot(111)
a.plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, frame2) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().grid(row=4, column=0, columnspan=2, sticky="nswe")
def on_configure(event):
canvasF2.configure(scrollregion=canvasF2.bbox('all'))
root.bind('<Configure>', on_configure)
root.mainloop()
I'm not sure why this error triggers when the window is closed, my guess is somehow after closing the window the widget canvasF2 is not getting destroyed properly. So if we destroy canvasF2 properly before closing the window the error won't trigger. I think there'll be a better way of doing this but here's what I did.
I handled the deletion of the window by using protocol method. I added this to the end of your code and I got no error by destroying canvasF2 before the main window is destroyed.
def close_window():
canvasF2.destroy()
root.destroy()
root.protocol("WM_DELETE_WINDOW", close_window)
Complete Code:
import tkinter as tk
from matplotlib.figure import Figure
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root=tk.Tk()
vscrollbar = tk.Scrollbar(root)
canvasF2= tk.Canvas(root,yscrollcommand=vscrollbar.set)
vscrollbar.config(command=canvasF2.yview)
vscrollbar.pack(side=tk.RIGHT, fill=tk.Y)
frame2=tk.Frame(canvasF2) #Create the frame which will hold the widgets
canvasF2.pack(side="left", fill="both", expand=True)
##Updated the window creation
canvasF2.create_window(0,0,window=frame2, anchor='nw')
#
fig = Figure(figsize=(10, 4), dpi=100)
t = np.arange(0, 3, .01)
a = fig.add_subplot(111)
a.plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, frame2) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().grid(row=4, column=0, columnspan=2, sticky="nswe")
def on_configure(event):
canvasF2.configure(scrollregion=canvasF2.bbox('all'))
root.bind('<Configure>', on_configure)
def close_window():
canvasF2.destroy()
root.destroy()
root.protocol("WM_DELETE_WINDOW", close_window)
root.mainloop()
Hope this helps!
I'm trying to set up some program that includes a matplotlib graph and tkinter buttons and whatnot below it. However, the matplotlib graph occupies the entire window overlaying the tkinter buttons and stuff.
I'd tried using pack, but it doesn't let me put stuff side by side, so I'd like the tkinter widgets to be arranged with .grid or coordinates.
from tkinter import *
from tkinter import StringVar
import tkinter
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
def eggs():
print("eggs")
root = Tk()
root.geometry("600x600")
root.title("eggs")
fig = Figure(figsize=(10, 6), dpi=100)
x = [1,2,3,4]
y = [1,2,3,4]
AS = [10/2**0]
fig.add_subplot(111).plot(x,y)
#fig.add_subplot(111).plot(AS)
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
testbutton = Button(root, text = "test button", command = eggs)
testbutton.place(x=100, y=550)
root.mainloop()
Wanting the top part of the window to be occupied by the graph and the buttons and other additional stuff below it.
You can use one Frame to keep graph and its toolbar vertically, and another Frame to keep buttons horizontally. And then you can use pack() to put one Frame at top top and other at the bottom.
The only problem makes figsize=(10, 6) which needs more space than "600x600"
BTW: you can use Button(toolbar, ...) to add button to NavigationToolbar2Tk - see "my tool".
]1
import tkinter as tk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
# --- functions ---
def eggs():
print("eggs")
# --- main ---
x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
AS = [10/2**0]
# ---
root = tk.Tk()
root.geometry("600x600")
root.title("eggs")
# ---
frame_top = tk.Frame(root)
frame_top.pack(fill='both', expand=True)
fig = Figure(dpi=100) # figsize=(10, 6),
fig.add_subplot(111).plot(x,y)
#fig.add_subplot(111).plot(AS)
canvas = FigureCanvasTkAgg(fig, master=frame_top) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(fill='both', expand=True)
toolbar = NavigationToolbar2Tk(canvas, frame_top)
toolbar.update()
tool = tk.Button(toolbar, text="my tool")
tool.pack(side='left')#, fill='x', expand=True)
# ---
frame_bottom = tk.Frame(root)
frame_bottom.pack(fill='x')
button1 = tk.Button(frame_bottom, text="button1")
button1.pack(side='left', fill='x', expand=True)
button2 = tk.Button(frame_bottom, text="button2")
button2.pack(side='left', fill='x', expand=True)
button3 = tk.Button(frame_bottom, text="button3")
button3.pack(side='left', fill='x', expand=True)
# ---
root.mainloop()