Python 如何修改此tkinter GUI代码以包含类和方法?

Python 如何修改此tkinter GUI代码以包含类和方法?,python,macos,matplotlib,user-interface,tkinter,Python,Macos,Matplotlib,User Interface,Tkinter,我有一个功能齐全的tkinter GUI(或者看起来是这样的…),但是我想通过为这些类创建类和方法来清理代码。是否有人能够浏览这些代码并将其重新组织为类和方法,使其仍以相同的方式运行,但如果可能,您是否也可以在一定程度上注释/包含注释?我希望这也是我的一次学习经历 在程序中引起问题的另一个原因是当我尝试使用quit按钮时python冻结。我不相信这是quit按钮代码的结果,因为在添加quit函数之前,当我简单地关闭GUI窗口时,python也有同样的问题。这是我在mac电脑上的结果吗?也许是与此

我有一个功能齐全的tkinter GUI(或者看起来是这样的…),但是我想通过为这些类创建类和方法来清理代码。是否有人能够浏览这些代码并将其重新组织为类和方法,使其仍以相同的方式运行,但如果可能,您是否也可以在一定程度上注释/包含注释?我希望这也是我的一次学习经历

在程序中引起问题的另一个原因是当我尝试使用quit按钮时python冻结。我不相信这是quit按钮代码的结果,因为在添加quit函数之前,当我简单地关闭GUI窗口时,python也有同样的问题。这是我在mac电脑上的结果吗?也许是与此相关的一些功能问题?或者这是一个可以在我的代码中解决的问题

另一方面,如果有人知道我可以用mac上的GUI做什么很酷的美学事情,请用想法回答!该程序当前从一个主窗口开始,该窗口有一个描述性标签、一个图形选项组合框、一个按钮,该按钮启动一个新窗口,其中包含要查看的与此建模数据相关的选定图形、一个向窗口添加一些pzazz的gif以及一个退出按钮。我使用的是arc主题,这使它看起来更漂亮,但我欢迎任何建议或想法,特别是在作图方面,因为我只有数据来制作条形图,但如果我可以用离散的定性数据来制作气泡图或类似的东西,我会很喜欢

谢谢你的帮助

这是我的密码:

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()

看来你是在要求一份工作。我不太明白你到底想在这里干什么。有很多方法可以在类中使用方法进行操作。通常,当类体系结构帮助您的代码运行、帮助您构建代码或帮助其他人阅读代码时,您会这样做。这段代码在这些方面似乎很成功,所以我很难为您提供一个好的类结构。如果你心中有一些目标,而不仅仅是使用课堂,请随意解释,我将尝试与你合作。至于退出按钮;是的,tk和mac有一个奇怪的东西…如果你删除除根以外的所有内容,它会起作用吗?用类重新编写它背后的想法是,我学习python的课程强调在使用tkinter和GUI时使用面向对象编程与类,因为它被认为是良好的形式。然而,我还没有掌握像使用面向任务编程那样使用类的诀窍——因此,我可以在没有类的情况下实现这一点。因此,重组的目的是帮助我更好地理解如何实现类。至于根问题,不,当我在程序中只有根时不会发生任何事情,代码将无错误地运行,但python不会启动GUI