Currently, I have a TKinter window that opens with a Pandastable table of data and five buttons at the bottom. Four of the buttons are intended to be used such that a user can click on a row in the table and then click on one of the four buttons to open a plot of data for that row's ID number (each row has a unique ID number). I can get the plot to show up as a Matplotlib window, but that causes two problems.
- When I close the TKinter root program with the Quit button, it leaves the Matplotlib window open and the app does not shut down until that is closed.
- The user of the app can click any of the four buttons any number of times and a new Matplotlib window will open, which may cause confusion.
Here is the click event code and the code for one button with its corresponding function:
self.button1 = tk.ttk.Button(self.root, width=25, text="Plot 0")
self.button1.pack(side="left")
def func1(self, dictionary):
df = pd.DataFrame.from_dict(dictionary)
ax = df.boxplot(column='RSSI', by='SenderID')
ax.set_title('Antenna A Ch 1 RSSI')
plt.show()
def clicked(self, event):
try:
rclicked = self.pt.get_row_clicked(event)
cclicked = self.pt.get_col_clicked(event)
clicks = (rclicked, cclicked)
except:
print ('Error')
if clicks:
try:
ROW = self.pt.model.getRecordAtRow(clicks[0])
self.button1.configure(command= lambda: self.func1(
self.unfiltered_df.loc[self.unfiltered_df['ReceiverID'] == ROW[0]]))
except:
print ('No record at:', clicks)
I have tried changing "func1" to create the figure in a canvas, but it puts it at the bottom of the root window in the middle of the buttons and looks disastrous.
Here is the function code for that button:
def func2(self, dictionary):
self.button1['state'] = 'disabled'
fig, ax = plt.subplots()
df = pd.DataFrame.from_dict(rssi_dict)
df.boxplot(ax=ax, column='RssiAntAChannel0', by='SenderDltID')
ax.set_title('Antenna A Ch 0 RSSI')
canvas = FigureCanvasTkAgg(fig) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.root.protocol("WM_DELETE_WINDOW", lambda :
self.button1.configure(state='normal') or self.root.destroy())
It solves the problem in that when the TKinter root program is closed, the plot is as well, but it's hardly an answer. It also prevents more than one plot from being opened, but I can't get the button to enable after first use.
The end result I'm looking for is a plot in a separate window like "func1" that disables the button until the window is closed. If that is not possible, a better looking plot in the root window that does the same thing would suffice.
