我创建了一个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文件通信了。我做错了什么
如果使用
os.system()
或使用模块subprocess
运行脚本,则不能使用其他脚本中的变量。它们作为独立的进程运行,不能共享变量(或内存中的数据)只能将一些文本值作为参数发送
然后您可以使用
sys.argv
在NoShow_Calc
中获取它如果需要其他变量,则必须以相同的方式发送其他值
及
但是使用
os.system()
不能将结果booked, arrived, vlookup
从NoShow_Calc
发送到NoShowGUI
你可以用
subprocess
来做,但它只能以文本的形式发送,所以NoShow_Calc
必须使用print()
来显示所有结果,并且NoShowGUI
必须将该文本解析为预期的结构-即列表、字典、DataFrame
最好使用
import
从NoShow_Calc.py
加载代码,然后所有代码都在同一进程中运行,这样所有代码都可以访问相同的变量,而不需要转换为文本或从文本返回为了更好,我在函数中添加了代码
然后在
NoShowGUI
中,您可以导入它并像任何其他函数一样使用它编辑:
我制作了最小的工作代码。我把它减少到只有一个文件名
NoShow_Calc.py
NoShowGUI.py
相关问题 更多 >
编程相关推荐