我创建了一个python脚本( NoShowCalc.py
)它使用3个选定的excel文件自动进行数据清理和分析( booked_file_path
, arrived_file_path
,和 vlookup_file_path
). 但是,我希望这一切都是通过gui执行的,所以我开始了一个单独的( GUI.py
)脚本来创建一个带有浏览按钮的界面,该按钮将获取这些文件路径名,然后将获取执行 NoShowCalc.py
脚本。一旦选择了这些excel文件,就会有另一个按钮来执行 NoShowCalc.py
脚本。然而,我做到了,而且成功了!但我不知道我改变了什么,现在两个不同的py文件没有连接。
这是书里的剧本 NoShowGUI.py
脚本:
def open_file():
browse_text.set('Loading...')
booked_file_path = askopenfile(parent=root, mode='rb', title='Choose a file', filetype=[('CSV file', '*.csv')])
if booked_file_path:
read_csv = (booked_file_path)
browse_text.set('Loaded')
def run():
os.system('NoShow_Calc.py')
calculate_text.set("Calculating...")
# Calculate button
calculate_text = tk.StringVar()
calculate_btn = tk.Button(root, textvariable=calculate_text, command=lambda:run(), font='Calibri', fg='black', height=1, width=15)
calculate_text.set("Calculate No Show")
calculate_btn.grid(column=2, row=9)
这是第一行 NoShowCalc.py
脚本:
import pandas as pd
booked = pd.read_csv(booked_file_path, parse_dates=['Appointment Date'])
arrived = pd.read_csv(arrived_file_path, parse_dates=['Appointment Date'])
vlookup = pd.read_excel(vlookup_file_path)
不断出现的错误是 NameError: name 'booked_file_path' is not defined
. 我不知道它以前是如何运行的,现在这个错误突然出现,因为它不能再与另一个py文件通信了。我做错了什么?
1条答案
按热度按时间wztqucjr1#
如果使用
os.system()
或使用模块subprocess
那么就不能使用其他脚本中的变量。它们作为独立的进程运行,不能共享变量(或内存中的数据)只能将一些文本值作为参数发送
然后你就可以进去了
NoShow_Calc
使用sys.argv
```import pandas as pd
import sys
booked_file_path = sys.argv[1]
booked = pd.read_csv(booked_file_path, parse_dates=['Appointment Date'])
arrived = pd.read_csv(arrived_file_path, parse_dates=['Appointment Date'])
vlookup = pd.read_excel(vlookup_file_path)
os.system('NoShow_Calc.py ' + booked_file_path + ' ' + other_filename)
booked_file_path = sys.argv[1]
other_filename = sys.argv[2]
etc.
import pandas as pd
def my_function(booked_file_path, arrived_file_path, vlookup_file_path):
booked = pd.read_csv(booked_file_path, parse_dates=['Appointment Date'])
arrived = pd.read_csv(arrived_file_path, parse_dates=['Appointment Date'])
vlookup = pd.read_excel(vlookup_file_path)
from NoShow_Calc import my_function
booked, arrived, vlookup = my_function(booked_file_path, arrived_file_path, vlookup_file_path)
import pandas as pd
def calc(booked_file_path): #, arrived_file_path, vlookup_file_path):
booked = pd.read_csv(booked_file_path, parse_dates=['Appointment Date'])
#arrived = pd.read_csv(arrived_file_path, parse_dates=['Appointment Date'])
#vlookup = pd.read_excel(vlookup_file_path)
import tkinter as tk
from tkinter.filedialog import askopenfilename # instead of
askopenfile
adding directory with this script to
sys.path
beforeimport NoShow_Calc
to make sure that
import
will searchNoShow_Calc.py
in correct folder even when GUI will be run from different folderimport os
import sys
HOME_DIR = os.path.dirname(os.path.abspath(file))
sys.path.append(HOME_DIR)
import NoShow_Calc
print('HOME_DIR:', HOME_DIR)
def select_filename():
global booked_file_path # inform function that it has to assign value to external/global variable
def run():
text_log.insert('end', "Calculating...\n")
--- main ---
booked_file_path = None # default value at start (so in
run
I can checkNone
to see if I selecte filename)arrived_file_path = None
vlookup_file_path = None
root = tk.Tk()
text_log = tk.Text(root)
text_log.grid(column=0, row=0)
select_btn = tk.Button(root, text="Select File Name", command=select_filename)
select_btn.grid(column=0, row=1)
calculate_btn = tk.Button(root, text="Calculate", command=run)
calculate_btn.grid(column=0, row=2)
root.mainloop()