Issue
I have window where I have a 'show and search' button. When I press the button, I want to show a live graph on a 2nd window (the show and search window), but I fail to plot the chart on it. Below is my code.
from tkinter.ttk import Combobox
from tkinter import *
import pandas as pd
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
class controller():
def __init__(self, container,model):
self.container = container
self.model=model()
self.container.title('Jig run count')
self.container.geometry('280x140')
def set_up(self):
self.add_remove_bt.place(x=10, y=50)
self.count_search_bt = Button(self.container, text=' SHOW & SEARCH ',
command=self.show_and_search)
self.count_search_bt.place(x=140, y=50)
def show_and_search(self):
show_and_search_windows=Tk()
show_and_search_form=Tk.winfo_toplevel(show_and_search_windows)
show_and_search_form.title("jig status and search")
show_and_search_form.geometry('800x500')
self.graph_area=Frame(show_and_search_form,width=450, height=300).place(x=5,y=5)
self.search_area=Frame(show_and_search_form,width=300, height=300).place(x=460,y=5)
self.table_area=Frame(show_and_search_form,width=760, height=100).place(x=5,y=310)
self.fig, self.ax1=plt.subplots()
self.canvas = FigureCanvasTkAgg(self.fig, self.graph_area)
ani=animation.FuncAnimation(self.fig,self.plot_chart,interval=1000)
self.canvas_widget = self.canvas.get_tk_widget()
self.canvas_widget.pack()
def plot_chart(self):
df=pd.read_csv(self.model.df_path,index_col='Type')
FAT_df=df.loc['FAT'].sort_values(by="running_time",ascending=False)
print(FAT_df)
x1=FAT_df['jig_name'].values
y1=FAT_df['running_time'].values
print(x1)
print(y1)
self.ax1.clear()
self.ax1.bar(x1,y1,label="top jigs")
self.ax1.show()
Solution
Note that self.graph_area
, self.search_area
and self.table_area
are all None
because they are result of .place(...)
, so widgets created as children of them will be children of root window instead actually.
Also show_and_search_form
is the same as show_and_search_windows
actually.
Below is the modified show_and_search()
to fix the above issues:
class controller():
...
def show_and_search(self):
# better use `Toplevel()` instead of `Tk()` for child window
show_and_search_form = Tk()
show_and_search_form.title("jig status and search")
show_and_search_form.geometry('800x500')
# call .place(...) in separate line
self.graph_area=Frame(show_and_search_form, width=450, height=300)
self.graph_area.place(x=5, y=5)
self.search_area=Frame(show_and_search_form, width=300, height=300, bg="cyan")
self.search_area.place(x=460, y=5)
self.table_area=Frame(show_and_search_form, width=750+5, height=100, bg="gold")
self.table_area.place(x=5, y=310)
self.fig, self.ax1 = plt.subplots()
self.canvas = FigureCanvasTkAgg(self.fig, self.graph_area)
# used instance variable self.ani instead of local variable
# to prevent garbage collection
self.ani = animation.FuncAnimation(self.fig, self.plot_chart, interval=1000)
self.canvas_widget = self.canvas.get_tk_widget()
self.canvas_widget.pack()
...
Answered By - acw1668
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.