End goal: When a user drags a folder to the GUI, the GUI becomes dimmed. My end goal is to have a plus icon or something that implies the user that you can drop it there. You know how you have them on webpages and some programs.
Currently, the screen doesnt become dimmed when you hover over with a folder. It does get dimmed after you place it.
This is the code:
# Function to handle file drop
def on_drop(event):
global input_file
input_file = event.data.strip('{}') # Remove curly braces if present
input_file_entry.delete(0, tk.END)
input_file_entry.insert(0, input_file)
window.configure(bg='#f0f0f0') # Restore original background color after drop
# Function to dim the screen during drag
def on_drag_enter(event):
window.configure(bg='#444444') # Dim the background
# Function to reset the screen when drag leaves
def on_drag_leave(event):
window.configure(bg='#f0f0f0') # Restore original background color
# GUI
window = TkinterDnD.Tk() # Setting the main window as DnD
And at the end of the file you have the bind drag and drop events >
# Bind drag-and-drop events
window.drop_target_register(DND_FILES)
window.dnd_bind('<<Drop>>', on_drop) # To be able to drop a folder in the text box
window.dnd_bind('<<DragEnter>>', on_drag_enter)
window.dnd_bind('<<DragLeave>>', on_drag_leave)
I expected the result.
My entire code:
import re
import tkinter as tk
from tkinter import messagebox, filedialog
import customtkinter as ctk
from tkinterdnd2 import TkinterDnD, DND_FILES # Load drag and drop
from PIL import Image, ImageTk
# Default file paths and patterns
input_file = ""
output_file = ""
words_to_remove = ["__label__sports"]
regex_to_remove = [r'[^\w\süğıöçŞİĞÜÇÖ\d\s]+']
# State variables for checkboxes
remove_numbers = False
remove_non_letters = False
add_commas = False
def toggle_remove_numbers():
global remove_numbers
remove_numbers = not remove_numbers
def toggle_remove_non_letters():
global remove_non_letters
remove_non_letters = not remove_non_letters
def toggle_add_commas():
global add_commas
add_commas = not add_commas
# Function to process text
def process_text():
global input_file, output_file
input_file = input_file_entry.get()
output_file = output_file_entry.get()
with open(input_file, 'r', encoding='utf-8') as file:
text = file.read()
for word in words_to_remove:
text = text.replace(word, '')
for pattern in regex_to_remove:
text = re.sub(pattern, '', text)
if remove_numbers:
text = re.sub(r'\d', '', text) # Remove numbers if checkbox is checked
if remove_non_letters:
text = re.sub(r'[^a-zA-Z\s]', '', text) # Remove non-letter characters if checkbox is checked
if add_commas:
words = text.split()
text = '\n'.join([f"{word}," for word in words])
with open(output_file, 'w', encoding='utf-8') as file:
file.write(text)
word_count = len(text.split())
messagebox.showinfo("Task Complete", f"The process is complete.\nWord count: {word_count}")
# Add word to remove list
def add_word_to_remove():
word = entry.get()
if word:
words_to_remove.append(word)
entry.delete(0, tk.END)
# Open file dialog for input file selection
def select_input_file():
global input_file
input_file = filedialog.askopenfilename()
input_file_entry.delete(0, tk.END)
input_file_entry.insert(0, input_file)
# Open file dialog for output file selection
def select_output_file():
global output_file
output_file = filedialog.asksaveasfilename()
output_file_entry.delete(0, tk.END)
output_file_entry.insert(0, output_file)
# Function to handle file drop
def on_drop(event):
global input_file
input_file = event.data.strip('{}') # Remove curly braces if present
input_file_entry.delete(0, tk.END)
input_file_entry.insert(0, input_file)
window.configure(bg='#fff') # Restore original background color after drop
# Function to dim the screen during drag
def on_drag_enter(event):
window.configure(bg='#444444') # Dim the background
# Function to reset the screen when drag leaves
def on_drag_leave(event):
window.configure(bg='#fff') # Restore original background color
# Function to open settings window
def open_settings():
settings_window = tk.Toplevel(window)
settings_window.title("Settings")
# this removes the maximize button
settings_window.resizable(0,0)
# Calculate the screen width and height
screen_width = settings_window.winfo_screenwidth()
screen_height = settings_window.winfo_screenheight()
# Calculate the x and y coordinates for the center of the screen
x_coordinate = (screen_width - 350) // 2
y_coordinate = (screen_height - 75) // 2
# Set the position of the Tk root window to the center
settings_window.geometry(f'350x75+{x_coordinate}+{y_coordinate}')
# Checkbox for removing numbers
remove_numbers_checkbox_var = tk.BooleanVar()
remove_numbers_checkbox = ctk.CTkCheckBox(
settings_window,
text="0-9",
variable=remove_numbers_checkbox_var,
command=toggle_remove_numbers,
text_color=('#252525', '#f4f4f4'),
border_width=2,
border_color='#e6e6e6',
checkbox_height=20,
checkbox_width=20,
hover='#fff'
)
remove_numbers_checkbox.grid(row=0, column=0, padx=10, pady=5)
# Checkbox for removing non-letter characters
remove_non_letters_checkbox_var = tk.BooleanVar()
remove_non_letters_checkbox = ctk.CTkCheckBox(
settings_window,
text="Non-Letters",
variable=remove_non_letters_checkbox_var,
command=toggle_remove_non_letters,
text_color=('#252525', '#f4f4f4'),
border_width=2,
border_color='#e6e6e6',
checkbox_height=20,
checkbox_width=20,
hover='#fff'
)
remove_non_letters_checkbox.grid(row=0, column=1, padx=20, pady=5)
# Checkbox for adding commas and new lines
add_commas_checkbox_var = tk.BooleanVar()
add_commas_checkbox = ctk.CTkCheckBox(
settings_window,
text="CSV",
variable=add_commas_checkbox_var,
command=toggle_add_commas,
text_color=('#252525', '#f4f4f4'),
border_width=2,
border_color='#e6e6e6',
checkbox_height=20,
checkbox_width=20,
hover='#fff'
)
add_commas_checkbox.grid(row=0, column=2, padx=10, pady=5)
# GUI
window = TkinterDnD.Tk() # Setting the main window as DnD
window.title('')
window.configure(bg='#fff')
# this removes the maximize button
window.resizable(0,0)
# Calculate the screen width and height
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
# Calculate the x and y coordinates for the center of the screen
x_coordinate = (screen_width - 350) // 2
y_coordinate = (screen_height - 450) // 2
# Set the position of the Tk root window to the center
window.geometry(f'350x450+{x_coordinate}+{y_coordinate}')
# Load image and create PhotoImage object
myImage = Image.open("RegExorcist_mac.png")
# Resize image
image_width = 100 # Set your desired width
image_height = 100 # Set your desired height
myImage = myImage.resize((image_width, image_height))
photo = ImageTk.PhotoImage(myImage)
# Get the background color of the window
window_bg_color = window.cget('bg')
# Display image without border and with matching border color
image_label = tk.Label(window, image=photo, bg=window_bg_color)
image_label.grid(row=1, column=0, columnspan=3, padx=10, pady=0)
# Add label for "RegExorcist" text
regex_label = ctk.CTkLabel(
window,
text="RegExorcist",
text_color=('#252525', '#f4f4f4'),
font=("Helvetica", 25, "bold")
)
regex_label.grid(row=2, column=0, columnspan=3, padx=10, pady=0)
# Add label for "version" text
version_label = ctk.CTkLabel(
window,
text="Version 0.0.3",
text_color=('#252525', '#f4f4f4'),
font=("Helvetica", 10)
)
version_label.grid(row=3, column=0, columnspan=3, padx=10, pady=0)
# Add settings button
settings_button = ctk.CTkButton(
window,
text="Settings",
command=open_settings,
fg_color=('#e6e6e6', '#3e3f3f'),
text_color=('#252525', '#f4f4f4'),
hover='#000'
)
settings_button.grid(row=10, column=0, columnspan=3, padx=10, pady=10)
# Input File Entry Widget
input_file_label = ctk.CTkLabel(
window,
text="Input File:",
text_color=('#252525', '#f4f4f4')
)
input_file_label.grid(row=4, column=0, padx=10, pady=5)
input_file_entry = ctk.CTkEntry(
window,
width=150,
fg_color='#fff',
border_color='#e6e6e6'
)
input_file_entry.grid(row=4, column=1, padx=10, pady=5)
input_file_button = ctk.CTkButton(
window,
text="Browse",
command=select_input_file,
fg_color=('#e6e6e6', '#3e3f3f'),
text_color=('#252525', '#f4f4f4'),
width=50,
hover='#000'
)
input_file_button.grid(row=4, column=2, padx=10, pady=5)
# Output File Entry Widget
output_file_label = ctk.CTkLabel(
window,
text="Output File:",
text_color=('#252525', '#f4f4f4')
)
output_file_label.grid(row=5, column=0, padx=10, pady=5)
output_file_entry = ctk.CTkEntry(
window,
width=150,
fg_color='#fff',
border_color='#e6e6e6'
)
output_file_entry.grid(row=5, column=1, padx=10, pady=5)
output_file_button = ctk.CTkButton(
window,
text="Browse",
command=select_output_file,
fg_color=('#e6e6e6', '#3e3f3f'),
text_color=('#252525', '#f4f4f4'),
width=50,
hover='#000'
)
output_file_button.grid(row=5, column=2, padx=10, pady=5)
# Text Entry Widget
label = ctk.CTkLabel(
window,
text="Specific:",
text_color=('#252525', '#f4f4f4')
)
label.grid(row=6, column=0, padx=10, pady=5)
entry = ctk.CTkEntry(
window,
width=150,
fg_color='#fff',
border_color='#e6e6e6'
)
entry.grid(row=6, column=1, padx=10, pady=5)
# Button to Add Word to Remove List
add_button = ctk.CTkButton(
window,
text="Add",
command=add_word_to_remove,
fg_color=('#e6e6e6', '#3e3f3f'),
text_color=('#252525', '#f4f4f4'),
width=50,
hover='#000'
)
add_button.grid(row=6, column=2, padx=10, pady=5)
# Button to Process Text at the bottom
process_button = ctk.CTkButton(
window,
text="Process Text",
command=process_text,
fg_color=('#e6e6e6', '#3e3f3f'),
text_color=('#252525', '#f4f4f4'),
hover='#000'
)
process_button.grid(row=8, column=0, columnspan=3, padx=10, pady=10)
# Bind drag-and-drop events
window.drop_target_register(DND_FILES)
window.dnd_bind('<<Drop>>', on_drop) # To be able to drop a folder in the text box
window.dnd_bind('<<DragInitCmd>>', on_drag_enter)
window.dnd_bind('<<DragEndCmd>>', on_drag_leave)
window.mainloop()
You bind on wrong events. <<DropEnter>>
and <<DropLeave>>
should be used instead:
window.dnd_bind('<<DropEnter>>', on_drag_enter)
window.dnd_bind('<<DropLeave>>', on_drag_leave)
Better to rename the two functions to on_drop_enter
and on_drop_leave
.