Skip to content

Instantly share code, notes, and snippets.

@cedrickvstheworld
Created June 1, 2018 15:13
Show Gist options
  • Save cedrickvstheworld/9b89e660b58f264e172297f959f30237 to your computer and use it in GitHub Desktop.
Save cedrickvstheworld/9b89e660b58f264e172297f959f30237 to your computer and use it in GitHub Desktop.
from tkinter import *
from tkinter import ttk
from tkinter import messagebox as mbox
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class Frame:
def __init__(self, root, framename, framelabel, col, row):
self.root = root
self.framename = framename
self.framelabel = framelabel
self.col = col
self.row = row
def createframe(self):
self.framename = ttk.LabelFrame(self.root, text=self.framelabel)
self.framename.grid(column=self.col, row=self.row, padx=5, pady=5, sticky=W)
class Msgbox:
def __init__(self, label, text):
self.label = label
self.text = text
def showbox(self):
mbox.showwarning(self.label, self.text)
class Widgets:
def __init__(self, framename, widgetname, col, row):
self.framename = framename
self.widgetname = widgetname
self.col = col
self.row = row
def grid(self):
self.widgetname.grid(column=self.col, row=self.row, padx=10, pady=5)
def createlabel(self, labeltext):
self.widgetname = ttk.Label(self.framename, text=labeltext, font='Rockwell')
self.grid()
def createtextbox(self, variable):
self.widgetname = ttk.Entry(self.framename, width=10, textvariable=variable)
self.grid()
def createbutton(self, btnlabel, command):
self.widgetname = ttk.Button(self.framename, width=10, text=btnlabel, command=command)
self.grid()
class Graph:
def __init__(self, root, width, height):
self.root = root
self.width = width
self.height = height
self.graphname = Figure(figsize=(self.width, self.height), facecolor='white')
canvas = FigureCanvasTkAgg(self.graphname, master=root)
canvas._tkcanvas.pack(fill=BOTH, expand=1, side=TOP)
class Plot:
def __init__(self, graphname, plotname, position):
self.graphname = graphname
self.plotname = plotname
self.position = position
self.plotname = self.graphname.add_subplot(self.position)
def drawinplot(self, xval, yval):
self.plotname.plot(xval, yval)
def configplot(self, xlabel, ylabel, xlimit, ylimit):
self.plotname.set_xlabel(xlabel)
self.plotname.set_ylabel(ylabel)
self.plotname.set_xlim(xlimit)
self.plotname.set_ylim(ylimit)
self.plotname.grid(linestyle='-')
xvalue = []
yvalue = []
def plotvals():
x = xVal.get()
y = yVal.get()
if len(xvalue) == 0:
if x > 2050:
errorB.showbox()
else:
xvalue.append(x)
yvalue.append(y)
else:
if x < xvalue[-1]:
errorA.showbox()
elif x > 2050:
errorB.showbox()
else:
xvalue.append(x)
yvalue.append(y)
graphA.plotname.clear()
graphA.plotname.plot(xvalue, yvalue)
graphA.configplot('Year', 'Sales in $', (2000, 2050), 0)
figure1.graphname.canvas.draw()
app = Tk()
app.title('Annual Income Graph Plotter')
app.resizable(0, 0)
graphframe = Frame(app, 'graphframe', 'Linear Graph', 0, 0)
graphframe.createframe()
drawingframe = Frame(app, 'drawingframe', 'Add Values', 0, 1)
drawingframe.createframe()
figure1 = Graph(graphframe.framename, 10, 4)
graphA = Plot(figure1.graphname, 'graphA', 111)
graphA.configplot('Year', 'Sales in $', (2000, 2050), 0)
hlabel = Widgets(drawingframe.framename, 'xlabel', 0, 0)
hlabel.createlabel('x Value:')
xVal = IntVar()
inputx = Widgets(drawingframe.framename, 'inputx', 1, 0)
inputx.createtextbox(xVal)
vlabel = Widgets(drawingframe.framename, 'ylabel', 2, 0)
vlabel.createlabel('y Value:')
yVal = IntVar()
inputy = Widgets(drawingframe.framename, 'inputx', 3, 0)
inputy.createtextbox(yVal)
errorA = Msgbox('Invalid Arguments', 'The value of x that you entered is less than\n'
' the previous value of x that is plotted')
errorB = Msgbox('Invalid Arguments', 'The value of x is out of range')
plotbtn = Widgets(drawingframe.framename, 'input', 4, 0)
plotbtn.createbutton('Plot Values', plotvals)
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment