Q: How to modify this tkinter GUI code to include classes and methods? I have a fully functioning tkinter GUI (or so it seems...) but I would like to clean up the code a bit by creating classes and methods for those classes. Would someone be able to go through this code and re-organise it into classes and methods so it still functions in the same way, but if possible could you also annotate/ include notes to a certain degree? I would like this to be a learning experience for me as well.
One other thing that is causing an issue in the program is python freezing when I try to use the quit button. I don't believe this is a result of the quit button code because python had the same issue when I simply closed the GUI window prior to adding the quit function. Is this a result of me being on a mac, and perhaps some functioning issues related to that? Or is this an issue that can be resolved in my code?
On a side note to this: If anyone knows of any cool aesthetic things I can do with a GUI on a mac please answer with ideas! This program currently starts at a main window that has a descriptive label, a combobox of graph options, a button that launches a new window with the selected graph to look at concerning the data this is modeled off of, a gif that adds some pzazz to the window, and a quit button. I am using the arc theme which has made it look nicer, but would welcome any suggestions or ideas, in particular with graphing because I only really have the data to make bar graphs it seems but would love to be making bubble plots or something cool like that if I could do so with discrete, qualitative data.
Thanks for any help!
Here's my code:
import tkinter as tk
from ttkthemes import ThemedStyle
from tkinter import *
from tkinter.ttk import *
from PIL import ImageTk,Image
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure

final_year_totals_df = get_totals_df(df, "Service Year")
regional_totals_df = get_totals_df(df, "Location")
ethnicity_totals_df = get_totals_df(df, "Ethnicity")
age_totals_df = get_totals_df(df, "Age band")
service_type_totals_df = get_service_df(df)
top_10_types_df = get_top_10(service_type_totals_df)
cause_totals_df = get_totals_df(df, "Cause of injury")
top_10_causes_df = get_top_10(cause_totals_df)

def pick_graphs():
    graph = ACC_combo.get()
    result = None
    title = None
    if graph == "Service Year":
        result = final_year_totals_df
        title = "ACC Concussion Claims by Year 2010-2019"
    elif graph == "Service Type":
        result = top_10_types_df
        title = "ACC Concussion Claims for Top 10 Treatment Service Types 2010-2019"
    elif graph == "Cause of Injury":
        result = top_10_causes_df
        title = "ACC Concussion Claims for Top 10 Causes of Injury 2010-2019"
    elif graph == "Location":
        result = regional_totals_df
        title = "ACC Concussion Claims by Region 2010-2019"
    elif graph == "Age Band":
        result = age_totals_df
        title = "ACC Concussion Claims by Age Bracket 2010-2019"
    elif graph == "Ethnicity":
        result = ethnicity_totals_df
        title = "ACC Concussion Claims by Ethnicity 2010-2019"
    return result, title

def display_graph():
    window = tk.Toplevel()
    window.geometry("1500x1200")

    style = ThemedStyle(window)
    style.set_theme("arc")

    graph, title = pick_graphs()
    fig = Figure(figsize = (3,3), dpi = 100)
    fig.subplots_adjust(left = 0.2)
    a = fig.add_subplot(111)
    a.xaxis.set_ticks_position('none')
    a.yaxis.set_ticks_position('none')
    a.xaxis.set_tick_params(pad=5)
    a.yaxis.set_tick_params(pad=10)
    a.grid(b=True, color='grey', linestyle='-.', linewidth=0.5, alpha=0.2)
    a.set_title(title, loc='left', pad=10)
    graph.plot(kind='barh', legend=False, ax=a, color='crimson')

    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    toolbar = NavigationToolbar2Tk(canvas, window)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    exit_button = Button(window, text = "Back To Main", command = window.destroy)
    exit_button.pack()


def _quit():
    root.quit()
    root.destroy()


root = Tk()
root.title('ACC Concussion Data 2010-2019')
root.geometry("600x500")

style = ThemedStyle(root)
style.set_theme("arc")

root_label = Label(root, text = 'Welcome to my GUI!' + '\n' + 
               'Please select the variable you would like to explore from the menu options below.' + 
               '\n' + 'The resulting pop-up window will display an interactive graph of a specific 
               aspect' + '\n' + 'of the data compiled by the ACC about concussion claims filed from 
               2010-2019.')
root_label.pack(side=TOP)

graph_button = Button(root, text = "Graph it!", command = display_graph)
graph_button.pack()

quit_button = Button(root, text="Exit", command=_quit)
quit_button.pack(side=BOTTOM)


ACC_combo = Combobox(root, values = ["Service Year", "Service Type", "Cause of Injury", 
                                                  "Location", "Age Band", "Ethnicity"])
ACC_combo.set("Service Year")
ACC_combo.pack()

photo = PhotoImage(file = "/Users/oscarevans/Desktop/concussion_GIF.gif")
label = Label(image = photo)
label.pack()

root.mainloop()

