i have a programme that takes user input for folder save location and a file selection to take data from
i have used tkinter's filedialog.askdirectory()
and askopenfilename()
to do these tasks as shown below:
import pandas as pd
from tkinter import Tk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from tkinter import *
############################################################### Reading Excel ###############################################################
print("\nSelect save folder:\n")
root = Tk()
root.withdraw()
savefilepath = filedialog.askdirectory()
print("\nSelect Rainfall .xlsx file:\n")
root2 = Tk()
root2.withdraw()
rainfallpath = askopenfilename()
rainfall = pd.read_excel(rainfallpath, sheet_name='Rainfall', usecols='B:D')
amounts = rainfall["Amount (mm)"]
range_pd = pd.read_excel(rainfallpath, sheet_name='Rainfall Statistics', usecols='K')
at the end of the script, however, the file browser and the root open unexpectedly as shown:
the final lines of my code export the results to the folder location:
with pd.ExcelWriter(savefilepath + "/Harvesting_Output.xlsx") as writer:
harvesting.to_excel(writer, sheet_name = "Harvesting")
harvesting_stats.to_excel(writer, sheet_name="Harvesting Statistics")
plt.show()
the programme works perfectly up until plt.show()
so i suspect this is causing the problem but im not sure why.
EDIT: After playing around with the position of the plt.show()
I have found that this is definitely the issue, but am still unsure as to why it is causing the root and file browser to open?
The following code will produce the issue using the data set i have linked after it:
import pandas as pd
import matplotlib.pyplot as plt
from tkinter import Tk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from tkinter import *
print("\nSelect save folder:\n")
root = Tk()
root.withdraw()
savefilepath = filedialog.askdirectory()
print("\nSelect Rainfall .xlsx file:\n")
rainfallpath = askopenfilename()
rainfall = pd.read_excel(rainfallpath, sheet_name='Rainfall', usecols='B:D')
amounts = rainfall["Amount (mm)"]
range_pd = pd.read_excel(rainfallpath, sheet_name='Rainfall Statistics', usecols='K')
range_end = range_pd.iat[0,0]
amounts.iloc[0:50].plot.line(y="Amount (mm)", x='Day', color='blue', legend=True)
plt.show()
https://www.dropbox.com/scl/fo/89y3vo465b38g3eewkmm7/h?dl=0&rlkey=imn0uin56gv81oo36in023y5g
why is this happening?