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? Very Happy



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()
Does the askyesno() in new() get triggered, at least? Can you throw some debug print statements in the code inside that conditional to make sure that it's getting properly run? When in doubt, trace and throw in debugging prints.
I've since figured it out; I had to alter the way the update method was called. Thanks guys Very Happy
leafiness0 wrote:
I've since figured it out; I had to alter the way the update method was called. Thanks guys Very Happy
No problem. Can you tell us more about this project, though, and keep us update? What is it about and for?
I'm working on a program for my fellowship at SUNY Stony Brook. The professor wants to see if we can make an electrical testing setup for under 100 bucks so that other departments in the physics department aren't limited by that 40000 dollar price tag for a generic setup. We certainly can't expect very high quality measurements from a 20 dollar Arduino UNO, but if we can get some decent graphs out of this, that'll be more than enough for our purposes.

If you want more information, I've made a recent blog post over here at my site detailing my current progress.
It's a pretty horrible set of tools, and it's my abhorred Java, but just for the sake of comparison, have you taken a look at Processing? It's in the same IDE family as the Arduino IDE, and apparently Processing apps interface well with the Arduino. I only know of it because I tested out an Arduino-based oscilloscope someone made that used Processing for the graphing frontend.
Yes, I believe I've looked over the exact same program! Unfortunately, it's far too slow for my purposes, and I also dislike Java like you do, so I went with Python Smile
leafiness0 wrote:
Yes, I believe I've looked over the exact same program! Unfortunately, it's far too slow for my purposes, and I also dislike Java like you do, so I went with Python Smile
A respectable decision, and I'm glad you picked the Python route. I just wanted to make sure you had checked all the applicable options to see what was right for you. Also, speaking of SUNY Stony Brook, that was one place I applied for my undergrad as a safety, and I visited again one summer to check out their MiNT lab, as I was working on MiNT at Binghamton. Smile
I should mention that you probably won't get better performance out of Python than you will from Java, unless you throw in some Cython modules and/or make very intelligent use of numpy.
elfprince13 wrote:
I should mention that you probably won't get better performance out of Python than you will from Java, unless you throw in some Cython modules and/or make very intelligent use of numpy.
I find that extremely debatable, and even if not, I would argue that the lower amount of wasted programmer time and improved maintainability are big wins for Python.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement