First you can define a function that takes to two arguments with a dataframe (df) and a status from your tkinter entry (BooleanVar).
global save_status
save_status = False
def save_file(data, status):
global save_status
save_status = status
if save_status:
file_path = filedialog.asksaveasfilename(defaultextension=".csv",
filetypes=[("csv file", ".csv")],
)
data.to_csv(file_path, sep = ";", index = False, decimal = ",")
Then you can call this function after df = pd.DataFrame(data)
, as shown in my case below:
data = np.array([mvol, mcurr])
data = data.transpose()
data = pd.DataFrame(data, columns = ["Voltage [V]", "Current [A]"])
# save file using status
save_file(data, self.save.get())
Here, self.save.get()
is boolean value whose change depends on the status of checkbox as shown below:
self.save = tk.BooleanVar(value = False)
save = ttk.Checkbutton(self, text = "Save Data",
variable = self.save, onvalue = True, offvalue = False)