I am new to Tkinter and Python as well. I have three buttons with commands in my Tkinter frame. Button 1 calls open_csv_dialog(), opens a file dialog box to select a .csv file and returns the path. Button 2 calls save_destination_folder(), opens a file dialog box to open the preferred directory and return the path.
My problem is with Button 3. It calls modify_word_doc() which needs the filepaths returned from button 1 and button 2.
I have tried;
button3 = ttk.Button(root, text="Run", command=lambda: modify_word_doc(open_csv_dialog, save_destination_folder)).pack()
but that obviously just prompts the file dialog box to open again for both the open_csv_dialog() and save_destination_folder() function which is undesired. I would like to just use the file path that was already returned from these two functions and pass it into modify_word_doc without being prompted by another file dialog box. I have also tried to use partial but I'm either using it wrong or it still has the same undesired consequences.
I have read the Tkinter docs about commands and searched SO for a possible answer, so apologies if this has been answered before and I failed to find it.
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import os
import csv
import docx
from functools import partial
root = tk.Tk()
def open_csv_dialog():
file_path = filedialog.askopenfilename(filetypes=(("Database files",
"*.csv"),("All files", "*.*")))
return file_path
def save_destination_folder():
file_path = filedialog.askdirectory()
return file_path
def modify_word_doc(data, location):
#data = open_csv_dialog()
#location = save_destination_folder()
#long code. takes .csv file path opens, reads and modifies word doc with
#the contents of the .csv, then saves the new word doc to the requested
#file path returned from save_destination_folder().
label = ttk.Label(root, text="Step 1 - Choose CSV File.",
font=LARGE_FONT)
label.pack(pady=10, padx=10)
button = ttk.Button(root, text="Choose CSV",
command= open_csv_dialog).pack()
label = ttk.Label(root,
text="Step 2 - Choose destination folder for your letters.",
font=LARGE_FONT)
label.pack(pady=10, padx=10)
button2 = ttk.Button(root, text="Choose Folder",
command=save_destination_folder).pack()
label = ttk.Label(root, text="Step 3 - Select Run.", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button3 = ttk.Button(root, text="Run",
command=lambda: modify_word_doc(open_csv_dialog, save_destination_folder)).pack()
root.mainloop()
(then).