我想创建一个GUI,以便用户可以输入参数并运行我的数据分析脚本。
我目前无法正常工作,但是由于某种原因,每次运行GUI时,整个脚本都会使用给定参数运行一次,因此我必须在屏幕打开之前对其进行预设,这会大大降低运行速度。
我的项目的结构是
-/ main.py -/ gui.py
,并且gui.py
在单击按钮后运行main.py
。
'main.py'是此版本的更长版本:
import pandas as pd
import numpy as np
from tabulate import tabulate
import paths
from zhou_utils import etl_utils as u
from zhou_utils import view_utils as v
import sys
# apply display options to see whole df in console
v.display_options()
def main(exam):
'''
Run the main script with extraction, operations, and visualization to output a closeout report.
:param exam: string with test exam code | <'MATH000'>
:return: void
'''
'''
Extraction
'''
# extract and clean raw exam download
exam_df = u.extract(subject=exam,
dir=paths.test_rcsd_downloads_dir)
[...]
'''
Operations
'''
df = pd.DataFrame()
# create DBN column
df['DBN'] = exam_df['School DBN']
[...]
'''
Visualization
'''
u.visualize(paths.project_dir,
f'{exam} Exam Closeout Report',
(df, 'Sheet1', 5),
template_dir='\Team\Formatting Templates'
)
if __name__ == "__main__":
main(sys.argv[1])
'gui.py'是这样的:
import tkinter as tk
import main
root = tk.Tk()
def run_script(exam):
subject = str(exam.get()) # change StrVar to string
# call main
main.main(subject)
# Add instructions
tk.Label(root, text="Exit out of all Excel applications before running.").grid(row=1, column=1, sticky='w')
# add entry bar for exam code
tk.Label(root, text="Enter exam code:").grid(row=2, column=1, sticky='w')
exam = tk.StringVar() # defines the widget state as string
tk.Entry(root,width=40, textvariable=exam).grid(row=2, column=2, columnspan=2)
exam.set("SXRP")
# add button to run script
tk.Button(root,text="Run", command=run_script(exam)).grid(row=3, column=3)
root.mainloop()
万一重要,gui.py
将与pyinstaller捆绑在一起,以便用户下载并运行它。