0

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()

2 Answers 2

1

You can use the text of the buttons as keys and the function references as the values of the dictionary dict_func as below:

dict_func = {"Open File": open_file,
             "Open Directory": open_directory}

Then create the buttons using the dict as below:

for name, func in dict_func.items():
    tk.Button(root, width=20, text=name, padx=5, pady=5, command=func).pack()
Sign up to request clarification or add additional context in comments.

Comments

0

The following code works, but there is one problem: after I selecting and execting one file, it automaticlly open folder.

import tkinter as tk
from data_check import DataCheck
from tkinter import filedialog, messagebox
import os

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 .xls .csv')])
    if file:
        DataCheck(file)
        
def open_directory():
    directory = filedialog.askdirectory()
    for filename in os.listdir(directory):
        if filename.endswith(".xlsx") or filename.endswith(".xls"):
            fullname = os.path.join(directory, filename)
            if fullname:
                DataCheck(fullname)

root = tk.Tk()

dict_func = {1: 'open_file',
             2: 'open_directory'
        }
for k, j in dict_func.items():
    b = tk.Button(root, width = 20, text = j, padx = 5, pady = 5, command = lambda: [open_file(), open_directory()])
    b.pack()

root.protocol('WM_DELETE_WINDOW', on_quit)
root.mainloop()

1 Comment

command = lambda: [open_file(), open_directory()] will call open_file() and then open_directory(). So what you said problem is expected result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.