I have created a tkinter window with Select file button to apply DataCheck function to excel files one by one with the following code:
import tkinter as tk
from tkinter import filedialog, messagebox
from data_check import DataCheck
def on_quit():
if messagebox.askokcancel('Confirmation', 'Do you really want to quit?'):
root.destroy()
def open_file():
file = filedialog.askopenfilename(filetypes=(('Excel Files', '.xlsx'),))
if file:
DataCheck(file)
root = tk.Tk()
btn = tk.Button(root, text='Select file', command=open_file)
btn.pack()
root.protocol('WM_DELETE_WINDOW', on_quit)
root.mainloop()
Let's say I want to add another button with the following function to open folders, how could I do that with tkinter? Many thanks.
def open_directory():
directory = filedialog.askdirectory()
for filename in os.listdir(directory):
if filename.endswith('.xlsx') or filename.endswith('.xls'):
fuullname = os.path.join(directory, filename)
if fullname:
DataCheck(fullname)
btn2 = tk.Button(root, text = 'Select folder', command = open_directory)
Code for reference:
root = Tk()
dict_words = {1: "open_file",
2: "open_directory"
}
for k, j in dict_words.items():
b = Button(root, width=20, text=j, padx=5, pady=5)
b.pack()
root.mainloop()