I am building a GUI application for plotting data. The program runs fine in Spyder and through command prompt, but after compiling it I noticed that the GUI opens up, accepts inputs but does not plot when I hit the "Plot" button. Instead it closes down unexpectedly. I've now simplified the code to the bare minimum that I think I need to do what I want. Please see the code below. I am using cx_Freeze to create my executable program. The code for the setup file is also included below. Any help would be much appreciated. Thanks.
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
from matplotlib.figure import Figure
class DataPlotter:
def __init__(self,master):
master.geometry('300x100+200+100')
master.title('Data Plotter')
master.resizable(False,False)
def PlotFigure(self,fig):
self.Window = tk.Toplevel(master)
self.canvas = FigureCanvasTkAgg(fig, self.Window)
self.canvas.show()
self.frametool = tk.Frame(self.Window)
self.toolbar = NavigationToolbar2TkAgg(self.canvas,self.frametool)
self.toolbar.update()
self.frametool.pack(side=tk.TOP, fill=tk.BOTH)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def Run(self): #
try:
app = Plotter(x=range(0,10), y=range(0,10))
PlotFigure(self,app.plotting())
except Exception as ex:
messagebox.showinfo('Caught Error','Exception caught: ' + str(ex))
ttk.Button(master, text='Plot', command=lambda:Run(self)).pack(anchor='center')
class Plotter(object):
def __init__(self, x=None, y=None):
self.x = x
self.y = y
def plotting(self):
fig = Figure()
ax = fig.add_subplot(111)
ax.plot(self.x, self.y, 'o-')
return fig
def main():
root = tk.Tk()
root.grab_set()
root.focus()
DataPlotter(root)
root.mainloop()
if __name__ == "__main__": main()
Setup file below:
from cx_Freeze import setup, Executable
import sys
import os
os.environ['TCL_LIBRARY'] = r'C:\ProgramData\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\ProgramData\Anaconda3\tcl\tk8.6'
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [Executable('Data_Plotter.py', base=base)]
build_exe_options = {"packages": ["os","tkinter","numpy","matplotlib"]}
setup(
name = "Data_Plotter",
options = {"build_exe": build_exe_options},
version = "1.0",
description = "This program plots data and displays the graph in a new window",
executables = executables
)