HOME/Articles/

matplotlib example gui (snippet)

Article Outline

Python matplotlib example 'gui'

Modules used in program:

  • import matplotlib as plt
  • import numpy as np
  • import pandas as pd
  • import matplotlib.animation as animation
  • import matplotlib
  • import tkinter as tk

python gui

Python matplotlib example: gui

import tkinter as tk
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import pandas as pd
import numpy as np
import matplotlib as plt



#Developing Starting Page
LARGE_FONT = ("Verdana", 12)
style.use("ggplot")




class SeaofBTCapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self, default="images.ico")
        tk.Tk.wm_title(self, "Solenco Power Box")
        container = tk.Frame(self)

        container.pack(side ="top", fill = "both", expand = True)

        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo, PageThree):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = "StartPage", font = LARGE_FONT)
        label.pack(pady = 10, padx  = 10)

        button = ttk.Button(self, text = 'Visit Page 1',
                            command = lambda:controller.show_frame(PageOne) )
        button.pack()

        button2 = ttk.Button(self, text = "Visit Page 2",
                            command = lambda: controller.show_frame(PageTwo))
        button2.pack()

        button3 = ttk.Button(self, text = "Graph Page",
                            command = lambda: controller.show_frame(PageThree))
        button3.pack()

class PageOne(tk.Frame):

    def __init__(self,parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = "Page One", font = LARGE_FONT)
        label.pack(pady = 10, padx = 10)

        button1 = ttk.Button(self, text = "Back to Home",
                            command = lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text = "Page Two",
                            command = lambda: controller.show_frame(PageTwo))
        button2.pack()



class PageTwo(tk.Frame):

    def __init__(self,parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = "Page Two", font = LARGE_FONT)
        label.pack(pady = 10, padx = 10)

        button1 = ttk.Button(self, text = "Back to Home",
                            command = lambda: controller.show_frame(StartPage))

        button1.pack()
        button2 = ttk.Button(self, text = "Page One",
                            command = lambda: controller.show_frame(PageOne))
        button2.pack()



class PageThree(tk.Frame):

    def __init__(self,parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = "Graph Page", font = LARGE_FONT)
        label.pack(pady = 10, padx = 10)

        button1 = ttk.Button(self, text = "Back to Home",
                            command = lambda: controller.show_frame(StartPage))

        button1.pack()
        f = Figure(figsize = (5,5), dpi = 100)
        a = f.add_subplot(111)
        csv_file = pd.read_csv('datasheet.csv')
        csv_file = csv_file.ix[41:, ['Output Voltage', 'Time']]
        csv_file = csv_file.set_index(['Time'])

        csv_file.plot(color='Green')
        a.plot ()










        canvas = FigureCanvasTkAgg(f, self)
        canvas.show()
        canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = True)

        toolbar = NavigationToolbar2TkAgg(canvas, self)
        toolbar.update()
        canvas._tkcanvas.pack(side = tk.TOP, fill = tk.BOTH, expand = True)

app = SeaofBTCapp()

app.mainloop()