import tkinter import tkinter.filedialog import re #import canvas2svg from math import * import graph class Application(tkinter.Frame): """Application class""" def __init__(self): tkinter.Frame.__init__(self, window) #Window settings window.title("Settings") window.resizable(0, 0) window.geometry("{}x{}+{}+{}".format( 550, 110, int((window.winfo_screenwidth() / 2) - 320), window.winfo_screenheight())) self.type = tkinter.StringVar() self.text = tkinter.StringVar() self.type.set("normal") self.graph = graph.Graph(20, 20) self.frame = self.graph.frame self.widgets() self.label() def widgets(self): """Draws widgets""" #Labels self.label1 = tkinter.Label(window, textvariable=self.text) self.label2 = tkinter.Label(window, text="Interval:") self.label3 = tkinter.Label(window, text="Color:") #Textboxes self.textbox1 = tkinter.Entry(window) self.textbox2 = tkinter.Entry(window) self.textbox3 = tkinter.Entry(window) #Options self.choose1 = tkinter.Radiobutton( window, value="normal", text="Normal", variable=self.type, command=self.label) self.choose2 = tkinter.Radiobutton( window, value="punti", text="Points", variable=self.type, command=self.label) self.choose3 = tkinter.Radiobutton( window, value="piecewise", text="Piecewise", variable=self.type, command=self.label) #Pulsanti self.button1 = tkinter.Button( window, text="Plot", command=self.plot) self.button2 = tkinter.Button( window, text="Clear", command=self.clear) self.button3 = tkinter.Button( window, text="Clean Canvas", command=self.graph.clean) self.button4 = tkinter.Button( window, text="Save", command=self.save) self.button5 = tkinter.Button( window, text="Exit", command=self.exit) #Widgets layout self.label1.grid( column=0, row=0, sticky="nw", pady=5, padx=5) self.label2.grid( column=0, row=1, sticky="nw", pady=2.5, padx=5) self.label3.grid( column=0, row=2, sticky="nw", pady=2.5, padx=5) self.textbox1.grid( column=1, row=0, sticky="nw", pady=5, padx=5) self.textbox2.grid( column=1, row=1, sticky="nw", pady=2.5, padx=5) self.textbox3.grid( column=1, row=2, padx=5, pady=2.5) self.choose1.grid( column=2, row=1, sticky="nw", pady=2.5, padx=5) self.choose2.grid( column=3, row=1, sticky="nw", pady=2.5, padx=5) self.choose3.grid( column=4, row=1, sticky="nw", pady=2.5, padx=5) self.button1.grid( column=2, row=0, sticky="nwes", pady=2.5, padx=5) self.button2.grid( column=3, row=0, sticky="nwes", pady=2.5, padx=5) self.button3.grid( column=4, row=0, sticky="nwes", pady=2.5, padx=5) self.button4.grid( column=2, row=2, sticky="nwes", pady=2.5, padx=5, columnspan=2) self.button5.grid( column=4, row=2, sticky="nwes", pady=2.5, padx=5) def label(self): """Change label basing on graph type""" if self.type.get() == "normal": self.text.set("f(x): y =") elif self.type.get() == "piecewise": self.text.set("[f1(x),(interval)],[f2(x),(interval)],...") else: self.text.set("(x1,y1),(x2,y2),...") def plot(self): """Plot the data""" #Converte la function in modo che possa essere interpretata da eval(). function = re.sub( "([\+-\-]?\d+)(x)", "\\1*x", self.textbox1.get().replace("^", "**")) function = re.sub( "(\|)(.+)(\|)", "abs(\\2)", function) function = re.sub( "(.+)(\!)", "factorial(\\1)", function) #Legge l'interval inserito. try: interval = eval(self.textbox2.get()) except SyntaxError: start = -self.graph.X stop = self.graph.X else: start = float(interval[0]) stop = float(interval[1]) #Legge il color inserito. color = self.textbox3.get() if color == "": color = "orange" #Disegna il graph in base al type inserito. if self.type.get() == "normal": function = eval("lambda x:" + function) self.graph.plot(function, start, stop, color) elif self.type.get() == "piecewise": functions = re.sub("(\[\()(.+?\))", "\\1lambda x:\\2", function) functions = re.sub("(\[)", "(", functions.replace("]", ")")) for function in eval(functions): self.graph.plot( function[0], function[1][0], function[1][1], color) else: function = graph.punti(eval(self.textbox1.get())) self.graph.plot( function, punti[0][0], punti[len(punti) - 1][1], color) def clear(self): """Clear entered data""" self.textbox1.delete(0, "end") self.textbox2.delete(0, "end") self.textbox3.delete(0, "end") def save(self): """Save plot in svg""" settings = { "parent": self.frame, "defaultextension": ".svg", "initialfile": "graph.svg", "title": "Salva il graph" } file = tkinter.filedialog.asksaveasfilename(**settings) canvas2svg.saveall(file, self.canvas) def exit(self): window.destroy() self.frame.destroy() window = tkinter.Tk() app = Application() app.mainloop()