Hey guys, I need a bit of help seeing as I'm new to Python. I'm using Matplotlib, and I'm trying to make it so whenever you click new in the menubar (Tkinter), it resets all the graphs. The program starts out with some random data that's already inputted, so if you look at the new() function it's supposed to clear all the data and redraw the graphs as blanks. The code isn't returning any errors, but it's not working either, so if anyone could help that would be great.
Also, anyone want to help optimizing my code?
Code:
Also, anyone want to help optimizing my code?
Code:
#import Tkinter modules
from Tkinter import*
import tkMessageBox
#import matplotlib modules
import matplotlib
matplotlib.use('TkAgg')
import numpy
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
#import pySerial
import serial
#define variables
digitalOutX = [1,3,5,7,9,13]
digitalOutY = [2,4,-1,2,3,2]
analogInX = [1,2,3,4,5,6,7,8,9,10,11,12,13]
analogInY = [2,46,256,68,43,12,4,65,68,78,7,3,23]
#menu functions
def quit():
if tkMessageBox.askyesno("Exit","Are you sure you want to exit? All unsaved data will be lost!"):
global root
root.destroy()
def new():
if tkMessageBox.askyesno("New","Create a new graph? All unsaved data will be lost!"):
digitalOutX=[]
digitalOutY=[]
analogInX=[]
analogInY=[]
graph.dgPlot.draw()
def about():
tkMessageBox.showinfo("About","This is a highly experimental project, designed to test ferroelectrics and capacitors via an Arduino.")
#serial functions
#GUI start
root = Tk();
root.title('Ferroscope')
root.resizable(width=FALSE, height=FALSE)
root.wm_iconbitmap('resources/icon.ico')
#add menubars
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=new)
filemenu.add_command(label="Save", command=quit)
filemenu.add_command(label="Settings", command=quit)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=about)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
#update matplotlib settings
matplotlib.rcParams.update({'font.size':7})
class plotCanvas(FigureCanvasTkAgg):
def __init__(self,x,y,title,xlabel,ylabel,master=None, width=5, height=3, dpi=100):
fig = Figure(figsize=(width,height), dpi=dpi, facecolor='k', edgecolor = 'none')
FigureCanvasTkAgg.__init__(self,figure = fig,master = master)
self.ax = fig.add_subplot(1,1,1)
self.ax.grid(color='gray',linestyle='dashed')
self.ax.plot(x,y,color='c')
self.ax.set_axis_bgcolor('k')
self.ax.set_title(title,color='w')
self.ax.set_xlabel(xlabel,color='w')
self.ax.set_ylabel(ylabel,color='w')
self.ax.spines['bottom'].set_color('w')
self.ax.spines['left'].set_color('w')
self.ax.tick_params(colors='w')
class graphRegion(Toplevel):
def __init__(self, master):
self.master = master
#initialize digitalOutput plot
self.dgPlot = plotCanvas(digitalOutX,digitalOutY,'Analog Output','time','voltage',master)
self.dgPlot.get_tk_widget().grid(row=0,column=0)
self.dgPlot.ax.set_ylim([-5,5])
self.dgPlot.ax.set_autoscaley_on(False)
#initialize analogInput plot
self.anPlot = plotCanvas(analogInX,analogInY,'Analog Input','time','voltage',master)
self.anPlot.get_tk_widget().grid(row=1,column=0)
graph = graphRegion(root)
#Tkinter event loop
root.mainloop()