fcp2021 / real_data / GUI_IMG.py
GUI_IMG.py
Raw
# Reference: 
# https://www.geeksforgeeks.org/open-a-new-window-with-a-button-in-python-tkinter/
# https://www.geeksforgeeks.org/how-to-embed-matplotlib-charts-in-tkinter-gui/
# https://stackoverflow.com/questions/41447065/tkinter-calling-function-when-button-is-pressed

from tkinter import * 
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, 
NavigationToolbar2Tk)

def GUI2image():    
    def realData(data):
        realdata = pd.read_csv('UK_full_covid_data.csv') 
        
        totalcase = realdata.iloc[335:,4].values
        posiCase = realdata.iloc[335:,5].values 
        newTest = realdata.iloc[335:,25].values 
        posiRate = realdata.iloc[335:,31].values 
    
        death = realdata.iloc[335:,8].values 
        totaldeaths = realdata.iloc[335:,7].values
        hospAdmission = realdata.iloc[335:,19].values  
        icu = realdata.iloc[335:,17].values
        
        totalVax = realdata.iloc[335:,34].values
        vax = realdata.iloc[335:,35].values
        fullyVax = realdata.iloc[335:,36].values
        newVax = realdata.iloc[335:,37].values
        
        if data == "case": return posiCase
        elif data == "totalcase": return totalcase
        elif data == "newTest": return newTest
        elif data == "posiRate": return posiRate
        
        elif data == "death": return death
        elif data == "totaldeaths": return totaldeaths
        elif data == "hosp": return hospAdmission
        elif data == "icu": return icu
        
        elif data == "totalVax": return totalVax
        elif data == "vax": return vax
        elif data == "fullyVax": return fullyVax
        elif data == "newVax": return newVax
    
    def xData(xAxis):
        if xAxis == "case": return np.arange(0, len(realData("case")))
        elif xAxis == "totalcase": return np.arange(0, len(realData("totalcase")))
        elif xAxis == "newTest": return np.arange(0, len(realData("newTest")))
        elif xAxis == "posiRate": return np.arange(0, len(realData("posiRate")))
        elif xAxis == "death": return np.arange(0, len(realData("death")))
        elif xAxis == "totaldeaths": return np.arange(0, len(realData("totaldeaths")))
        elif xAxis == "hosp": return np.arange(0, len(realData("hosp")))
        elif xAxis == "icu": return np.arange(0, len(realData("icu")))
        elif xAxis == "totalVax": return np.arange(0, len(realData("totalVax")))
        elif xAxis == "vax": return np.arange(0, len(realData("vax")))
        elif xAxis == "fullyVax": return np.arange(0, len(realData("fullyVax")))
        elif xAxis == "newVax": return np.arange(0, len(realData("newVax")))
      
    def yData(yAxis):
        if yAxis == "case": return realData("case")
        elif yAxis == "totalcase": return realData("totalcase")
        elif yAxis == "newTest": return realData("newTest")
        elif yAxis == "posiRate": return realData("posiRate")
        elif yAxis == "death": return realData("death")
        elif yAxis == "totaldeaths": return realData("totaldeaths")
        elif yAxis == "hosp": return realData("hosp")
        elif yAxis == "icu": return realData("icu")
        elif yAxis == "totalVax": return realData("totalVax")
        elif yAxis == "vax": return realData("vax")
        elif yAxis == "fullyVax": return realData("fullyVax")
        elif yAxis == "newVax": return realData("newVax")
        
    def choosePlot(pltVer):
        newWindow = Toplevel(window)
        newWindow.title("Covid-19 Related Graph")
        newWindow.geometry("800x600")
        newWindow.configure(bg="white")
        reminder = Label(newWindow, bg="white", text="Remember 3W's to reduce the risk of COVID-19 :D\n\n")
        reminder.pack()
        
        fig = plt.figure(figsize=(8, 7))
        ax_upper_left = fig.add_subplot(2.5, 2, 1)
        ax_upper_right = fig.add_subplot(2.5, 2, 2)
        ax_lower_left = fig.add_subplot(2.5, 2, 3)
        ax_lower_right = fig.add_subplot(2.5, 2, 4)
    
        def graph(ver):        
            if ver == "ver1":    
                textstr = "Source: UK_Full_Covid_Data (Date issued: 01/01/2021-29/03/2021)"
                line1, = ax_upper_left.plot(xData("case"), yData("case"), color='maroon')
                line2, = ax_upper_right.plot(xData("totalcase"), yData("totalcase"), color='firebrick')
                line3, = ax_lower_left.plot(xData("newTest"), yData("newTest"), color='indianred')
                line4, = ax_lower_right.plot(xData("posiRate"), yData("posiRate"), color='salmon')
                
                ax_upper_left.set_ylabel('Number of Positive Cases', fontsize=11)
                ax_upper_right.set_ylabel('Total Cases', fontsize=11)
                ax_lower_left.set_ylabel('New Testing', fontsize=11)
                ax_lower_right.set_ylabel('Positive Rate (%)', fontsize=11)
                
                fig.tight_layout()
                fig.suptitle("Positive Cases & New Testing")
                fig.subplots_adjust(top=0.92)
                plt.figtext(0.03, 0.01, textstr, fontsize=7.5)
        
                return fig, line1, line2, line3, line4
        
            elif ver == "ver2":
                textstr = "Source: UK_Full_Covid_Data (Date issued: 01/01/2021-29/03/2021)"    
                line1, = ax_upper_left.plot(xData("death"), yData("death"), color='darkblue')
                line2, = ax_upper_right.plot(xData("totaldeaths"), yData("totaldeaths"), color='blue')
                line3, = ax_lower_left.plot(xData("hosp"), yData("hosp"), color='royalblue')
                line4, = ax_lower_right.plot(xData("icu"), yData("icu"), color='cornflowerblue')
                
                ax_upper_left.set_ylabel('Number of Death', fontsize=11)
                ax_upper_right.set_ylabel('Total Deaths', fontsize=11)
                ax_lower_left.set_ylabel('Hospital Admission', fontsize=11)
                ax_lower_right.set_ylabel('ICU patients', fontsize=11)
                
                fig.tight_layout()
                fig.suptitle("Death Cases & Hospital Related")
                fig.subplots_adjust(top=0.94)
                plt.figtext(0.03, 0.01, textstr, fontsize=7.5)
        
                return fig, line1, line2, line3, line4,
                
            elif ver == "ver3":
                textstr = "Source: UK_Full_Covid_Data (Date issued: 01/01/2021-29/03/2021)"    
                line1, = ax_upper_left.plot(xData("vax"), yData("vax"), color='limegreen')
                line2, = ax_upper_right.plot(xData("totalVax"), yData("totalVax"), color='yellowgreen')
                line3, = ax_lower_left.plot(xData("fullyVax"), yData("fullyVax"), color='seagreen')
                line4, = ax_lower_right.plot(xData("newVax"), yData("newVax"), color='darkgreen')
                
                ax_upper_left.set_ylabel('Number of People Vaccinated', fontsize=11)
                ax_upper_right.set_ylabel('Total Number of People Vaccinated', fontsize=11)
                ax_lower_left.set_ylabel('Number of People Fully Vaccinated', fontsize=11)
                ax_lower_right.set_ylabel('Number of New Vaccinated', fontsize=11)
                
                fig.tight_layout()
                fig.suptitle("Vaccination")
                fig.subplots_adjust(top=0.93)        
                plt.figtext(0.03, 0.01, textstr, fontsize=7.5)
        
                return fig, line1, line2, line3, line4,
        
        if pltVer == 1:    
            graph("ver1")
            canvas1 = FigureCanvasTkAgg(fig, master = newWindow)  
            canvas1.draw()
            canvas1.get_tk_widget().pack()
            toolbar = NavigationToolbar2Tk(canvas1, newWindow)
            toolbar.update()
            canvas1.get_tk_widget().pack()
        
        elif pltVer == 2:    
            graph("ver2")
            canvas2 = FigureCanvasTkAgg(fig, master = newWindow)  
            canvas2.draw()
            canvas2.get_tk_widget().pack()
            toolbar = NavigationToolbar2Tk(canvas2, newWindow)
            toolbar.update()
            canvas2.get_tk_widget().pack()
            
        elif pltVer == 3:    
            graph("ver3")
            canvas3 = FigureCanvasTkAgg(fig, master = newWindow)  
            canvas3.draw()
            canvas3.get_tk_widget().pack()
            toolbar = NavigationToolbar2Tk(canvas3, newWindow)
            toolbar.update()
            canvas3.get_tk_widget().pack()
    
    def openPlot1(): choosePlot(1)
    def openPlot2(): choosePlot(2)
    def openPlot3(): choosePlot(3)
    
    window = Tk()
    window.title('COVID-19 Related Graph')
    window.geometry("500x285")
    window.configure(bg="white")
    
    instruction = Label(window, bg="white", text="Click the button to see the graph\n")
    instruction.pack()
    
    explanation = Label(window, bg="white", text="Each graph shows real data of\nCOVID-19 in the UK every day since 1st January 2021\n")
    explanation.pack()
    
    plot_button1 = Button(master = window, command = openPlot1, height = 2, width = 35, bg="white", text = "Positive Cases & New Testing")
    plot_button1.pack()
    
    plot_button2 = Button(master = window, command = openPlot2, height = 2, width = 35, bg="white", text = "Death cases & Hospital Related")
    plot_button2.pack()
    
    plot_button3 = Button(master = window, command = openPlot3, height = 2, width = 35, bg="white", text = "Vaccination")
    plot_button3.pack()
    
    button = Button(text = "\nClick and Quit\n", bg="white", height = 1, width = 15, command = window.destroy)
    button.pack()
    button.place(x=190, y=250)
      
    window.mainloop()