|
import tkinter as tk |
|
import tkinter.ttk as ttk |
|
from tkinter import filedialog |
|
import os |
|
import csv |
|
import random |
|
|
|
def filter_csv(input_file, output_file, _search_strings): |
|
output_directory = os.getcwd() |
|
output_file = os.path.join(output_directory, output_file) |
|
search_strings = [s.strip() for s in _search_strings.split(',')] |
|
with open(input_file, 'r', newline='', encoding='utf-8') as f_in, \ |
|
open(output_file, 'w', newline='', encoding='utf-8') as f_out: |
|
reader = csv.reader(f_in) |
|
writer = csv.writer(f_out) |
|
writer_count = 0 |
|
for row in reader: |
|
if all(search_str in value for search_str in search_strings for value in row): |
|
writer.writerow(row) |
|
writer_count += 1 |
|
return writer_count |
|
|
|
def open_file(): |
|
initial_dir = os.getcwd() |
|
filepath = filedialog.askopenfilename( |
|
initialdir=initial_dir, |
|
filetypes=[("CSV Files", "*.csv")] |
|
) |
|
if filepath: |
|
entry_file_path.delete(0, tk.END) |
|
entry_file_path.insert(0, filepath) |
|
|
|
def search(): |
|
global total_rows |
|
input_file = entry_file_path.get() |
|
keywords = entry_keyword.get() |
|
keyword_label.config(text="๊ฒ์ํ ํค์๋: ") |
|
keyword_label_mode.config(text="(ํ์ฌ ๊ฒ์ ๋ชจ๋๋ก ๋์ํ๊ณ ์์ต๋๋ค.)", fg="blue") |
|
output_file = 'txt2img_temp_prompt.csv' |
|
writer_count = filter_csv(input_file, output_file, keywords) |
|
total_rows = writer_count |
|
total_rows_count_label.config(text=f"Total Rows: {writer_count}") |
|
text_output.insert(tk.END, f"์ด {writer_count}๊ฐ์ ๋ฌธ์์ด์ด ๊ฒ์๋์ด ์ ์ฅ๋์์ต๋๋ค.\n") |
|
cached_rows = None |
|
|
|
def exclude(): |
|
global total_rows |
|
input_file = entry_file_path.get() |
|
keywords = entry_keyword.get() |
|
output_file = 'txt2img_temp_prompt.csv' |
|
keyword_label.config(text="๊ฒ์ํ ํค์๋: ") |
|
keyword_label_mode.config(text="(ํ์ฌ ์ ์ธ ๋ชจ๋๋ก ๋์ํ๊ณ ์์ต๋๋ค.)", fg="red") |
|
output_directory = os.getcwd() |
|
output_file = os.path.join(output_directory, output_file) |
|
search_strings = [s.strip() for s in keywords.split(',')] |
|
|
|
with open(input_file, 'r', newline='', encoding='utf-8') as f_in, \ |
|
open(output_file, 'w', newline='', encoding='utf-8') as f_out: |
|
reader = csv.reader(f_in) |
|
writer = csv.writer(f_out) |
|
writer_count = 0 |
|
for row in reader: |
|
if not any(search_str in value for search_str in search_strings for value in row): |
|
writer.writerow(row) |
|
writer_count += 1 |
|
total_rows = writer_count |
|
total_rows_count_label.config(text=f"Total Rows: {writer_count}") |
|
text_output.insert(tk.END, f"์ด {writer_count}๊ฐ์ ๋ฌธ์์ด์ด ๊ฒ์๋์ด ์ ์ฅ๋์์ต๋๋ค.\n") |
|
cached_rows = None |
|
|
|
|
|
def reset(): |
|
cached_rows = None |
|
entry_file_path.delete(0, tk.END) |
|
entry_keyword.delete(0, tk.END) |
|
entry_deep_search.delete(0, tk.END) |
|
text_output.delete('1.0', tk.END) |
|
|
|
def random_function(): |
|
global last_deep_search_keywords, cached_rows |
|
current_deep_search_keywords = entry_deep_search.get().strip() |
|
if current_deep_search_keywords != last_deep_search_keywords or not cached_rows: |
|
with open('txt2img_temp_prompt.csv', 'r', newline='', encoding='utf-8') as f: |
|
reader = csv.reader(f) |
|
if current_deep_search_keywords: |
|
keywords = current_deep_search_keywords.split(',') |
|
exclude_keywords = [s[1:].strip() for s in keywords if s.strip().startswith('~')] |
|
include_keywords = [s.strip() for s in keywords if not s.strip().startswith('~')] |
|
rows = [row for row in reader if not any(exclude in cell for exclude in exclude_keywords for cell in row)] |
|
cached_rows = [row for row in rows if all(include in cell for include in include_keywords for cell in row)] |
|
else: |
|
cached_rows = list(reader) |
|
last_deep_search_keywords = current_deep_search_keywords |
|
text_output.delete('1.0', tk.END) |
|
if cached_rows: |
|
random_index = random.randint(0, len(cached_rows) - 1) |
|
random_row = cached_rows.pop(random_index) |
|
text_output.insert(tk.END, f"{', '.join(random_row)}\n") |
|
else: |
|
text_output.insert(tk.END, "๊ฒ์ ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๊ฐ ์๊ฑฐ๋ CSV ํ์ผ์ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.\n") |
|
|
|
cached_rows_count_label.config(text=f"Cached Rows: {len(cached_rows)}") |
|
|
|
|
|
|
|
|
|
def copy_to_clipboard(): |
|
window.clipboard_clear() |
|
combined_text = entry_fixed_prompt.get() + "\n" + text_output.get("1.0", tk.END) |
|
window.clipboard_append(combined_text) |
|
|
|
def exit_program(): |
|
window.destroy() |
|
|
|
|
|
def save_settings(): |
|
with open('app_settings.txt', 'w', encoding='utf-8') as f: |
|
f.write(entry_file_path.get() + '\n') |
|
f.write(entry_keyword.get() + '\n') |
|
f.write(entry_deep_search.get() + '\n') |
|
|
|
def load_settings(): |
|
if os.path.exists('app_settings.txt'): |
|
with open('app_settings.txt', 'r', encoding='utf-8') as f: |
|
settings = f.readlines() |
|
entry_file_path.insert(0, settings[0].strip()) |
|
entry_keyword.insert(0, settings[1].strip()) |
|
entry_deep_search.insert(0, settings[2].strip()) |
|
|
|
def exit_program(): |
|
save_settings() |
|
window.destroy() |
|
|
|
def on_ctrl_enter(event): |
|
random_function() |
|
|
|
def on_shift_enter(event): |
|
random_function() |
|
copy_to_clipboard() |
|
|
|
window = tk.Tk() |
|
window.title("Prompt Selector for Danbooru tags") |
|
last_deep_search_keywords = None |
|
cached_rows = [] |
|
total_rows = 0 |
|
|
|
|
|
|
|
label_file_path = tk.Label(window, text="CSV ํ์ผ ๊ฒฝ๋ก:") |
|
label_file_path.grid(row=0, column=0, columnspan=2, sticky='w') |
|
entry_file_path = tk.Entry(window, width=70) |
|
entry_file_path.grid(row=1, column=0, columnspan=2, padx=5, pady=5) |
|
button_open_file = tk.Button(window, text="ํ์ผ ์ด๊ธฐ", command=open_file) |
|
button_open_file.grid(row=1, column=2, padx=5, pady=5) |
|
|
|
|
|
keyword_label = tk.Label(window, text="๊ฒ์ํ ํค์๋: ") |
|
keyword_label.grid(row=2, column=0, sticky='w') |
|
entry_keyword = tk.Entry(window, width=70) |
|
entry_keyword.grid(row=3, column=0, columnspan=2, padx=5, pady=5) |
|
keyword_label_mode = tk.Label(window, text="") |
|
keyword_label_mode.grid(row=2, column=1, sticky='w') |
|
|
|
|
|
frame_buttons = tk.Frame(window) |
|
frame_buttons.grid(row=4, column=0, columnspan=3, pady=10) |
|
|
|
|
|
button_search = tk.Button(frame_buttons, text="๊ฒ์", command=search) |
|
button_search.pack(side=tk.LEFT, padx=5) |
|
button_exclude = tk.Button(frame_buttons, text="์ ์ธ", command=exclude) |
|
button_exclude.pack(side=tk.LEFT, padx=5) |
|
button_reset = tk.Button(frame_buttons, text="๋ฆฌ์
", command=reset) |
|
button_reset.pack(side=tk.LEFT, padx=5) |
|
|
|
|
|
label_deep_search = tk.Label(window, text="์ฌ์ธต๊ฒ์ ํค์๋: ํค์๋ ์์ ~๋ฅผ ๋ถ์ด๋ฉด ์ ์ธํฉ๋๋ค.") |
|
label_deep_search.grid(row=5, column=0, columnspan=2, sticky='w') |
|
entry_deep_search = tk.Entry(window, width=70) |
|
entry_deep_search.grid(row=6, column=0, columnspan=2, padx=5, pady=5) |
|
|
|
|
|
cached_rows_count_label = tk.Label(window, text="Cached Rows: 0") |
|
cached_rows_count_label.grid(row=8, column=1, padx=5, pady=5) |
|
total_rows_count_label = tk.Label(window, text="Total Rows: 0") |
|
total_rows_count_label.grid(row=8, column=0, padx=5, pady=5) |
|
|
|
|
|
button_random = tk.Button(window, text="๋๋ค", command=random_function) |
|
button_random.grid(row=7, column=0, padx=5, pady=5, sticky='w') |
|
button_copy = tk.Button(window, text="๋ณต์ฌ", command=copy_to_clipboard) |
|
button_copy.grid(row=7, column=1, padx=5, pady=5) |
|
button_exit = tk.Button(window, text="์ข
๋ฃ", command=exit_program) |
|
button_exit.grid(row=7, column=2, padx=5, pady=5, sticky='w') |
|
|
|
|
|
text_output = tk.Text(window, height=10, width=70) |
|
text_output.grid(row=9, column=0, columnspan=3, padx=5, pady=5) |
|
|
|
fixed_prompt_label = tk.Label(window, text="๊ณ ์ ํ๋กฌํํธ: ๋ณต์ฌ ๋ฒํผ์ ๋๋ ์ ๋ ๋์ํฉ๋๋ค.") |
|
fixed_prompt_label.grid(row=10, column=0, columnspan=2, sticky='w') |
|
entry_fixed_prompt = tk.Entry(window, width=70) |
|
entry_fixed_prompt.grid(row=11, column=0, columnspan=2, padx=5, pady=5) |
|
|
|
shortcut_info_label = tk.Label(window, text="* Ctrl+Enter๋ฅผ ๋๋ฅด๋ฉด [๋๋ค] ๋ฒํผ์ด ๋์ํฉ๋๋ค.\n* Shift+Enter๋ฅผ ๋๋ฅด๋ฉด [๋๋ค] ๋ฒํผ์ด ๋์ํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๊ฐ ํด๋ฆฝ๋ณด๋์ ๋ณต์ฌ๋ฉ๋๋ค.", justify=tk.LEFT) |
|
shortcut_info_label.grid(row=13, column=0, columnspan=3, padx=5, pady=5, sticky='w') |
|
|
|
window.bind('<Control-Return>', on_ctrl_enter) |
|
window.bind('<Shift-Return>', on_shift_enter) |
|
|
|
load_settings() |
|
|
|
window.mainloop() |
|
|