我有两个按钮“浏览文件 2A”和“浏览文件 PR”,它们打开并读取选定的 excel 文件路径,我怎样才能使文件路径被读取(“打开”那些 excel 文件,以便我在save_slogan() 由变量 df1 和 df2 起作用?请参阅我的函数 save_slogan(),这里是我的代码:
from tkinter import *
# import filedialog module
from tkinter import filedialog
import pandas as pd
# Function for opening the
# file explorer window
from tkinter import *
import smtplib
def browseFiles1():
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
# Change label contents
label_file_explorer.configure(text="File Opened: "+filename)
def browseFiles():
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("Text files",
"*.txt*"),
("all files",
"*.*")))
# Change label contents
label_file_explorer.configure(text="File Opened: "+filename)
def save_slogan():
import pandas as pd
df1=#read and open the excel file from its file path from function browseFiles()
df2=#read and open the excel file from its file path from function browseFiles1()
xx1=df1.rename(columns={"Description":"Desc 2A","Doc Number":"Document Number"})
xx=df2.rename(columns={"Description":"Desc PR","Doc Number":"Document Number"})
combined = pd.merge(xx1, xx, how="outer", on="Document Number")
def case_code(row):
if row["Tax Amount 2A"] == row["Tax Amount PR"]:
return "exact"
elif pd.isna(row["Tax Amount 2A"]):
return "Addition in PR"
elif pd.isna(row["Tax Amount PR"]):
return "Addition in 2A"
elif row["Tax Amount 2A"] != row["Tax Amount PR"]:
return "mismatch"
codes=combined.apply(case_code, axis="columns")
answer = combined.assign(**{"Match type": codes})
final=answer[["Match type"] + [*combined.columns]]
final.to_excel('done1.xlsx')
# Create the root window
window = Tk()
# Set window title
window.title('File Explorer')
# Set window size
window.geometry("500x500")
#Set window background color
window.config(background = "white")
# Create a File Explorer label
label_file_explorer = Label(window,
text = "PR2A",
width = 100, height = 4,
fg = "blue")
button_explore = Button(window,
text = "Browse File 2A",
command = browseFiles)
button_explore1 = Button(window,
text = "Browse File PR",
command = browseFiles1)
button_explore2 = Button(window,
text = "Go and Save",
command = save_slogan)
# the widgets at respective positions
# in a table like structure by
# specifying rows and columns
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
button_explore1.grid(column=1, row=3)
button_explore2.grid(column=1,row=4)
# Let the window wait for any events
window.mainloop()
请帮忙