You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6 KiB

import tkinter as tk
from tkinter import messagebox, simpledialog, filedialog, CENTER, font
import json
from db import *
FIRST = 1000
class JsonEditorApp:
def __init__(self, root):
self.root = root
self.root.title("JSON Editor")
self.custom_font = font.Font(family="Helvetica", size=14)
self.data = read()
self.setup_ui()
def setup_ui(self):
self.key_var = tk.StringVar(value='0')
self.key_var.trace_add("write", self.on_key_change)
tk.Label(self.root, text="Select Type:", font=self.custom_font).pack(pady=5)
self.okButton = tk.Radiobutton(self.root, text="Words"
, variable=self.key_var, value='0', font=self.custom_font).pack(side=tk.TOP)
self.badButton = tk.Radiobutton(self.root, text="Bad Words"
, variable=self.key_var, value='1', font=self.custom_font).pack(side=tk.TOP)
self.search_var = tk.StringVar()
self.search_var.trace_add("write", self.on_search_change)
self.search_entry = tk.Entry(self.root, textvariable=self.search_var, font=self.custom_font)
self.search_entry.pack(pady=5)
self.search_entry.bind('<Control-a>', self.select_call)
self.search_entry.bind('<Return>', self.add_word)
self.search_entry.bind('<Control-s>', self.search_word)
self.root.bind('<Delete>', self.delete_word)
#button_frame = tk.Frame(self.root)
#button_frame.pack(pady=5)
#tk.Button(button_frame, text="Search Word", command=self.search_word, font=self.custom_font).pack(side=tk.LEFT, padx=5)
#tk.Button(button_frame, text="Delete Word", command=self.delete_word, font=self.custom_font).pack(side=tk.LEFT, padx=5)
self.listbox = tk.Listbox(self.root, height=10, width=50, font=self.custom_font)
self.listbox.pack(pady=5, padx=30, fill=tk.BOTH, expand=True)
self.listbox.configure(justify=CENTER)
self.update_listbox()
def select_call(self, event):
self.search_var.set('')
def on_search_change(self, *args):
if self.search_var.get() == '':
self.update_listbox()
def on_key_change(self, *args):
self.update_listbox()
def update_listbox(self):
self.listbox.delete(0, tk.END)
words = self.data.get(self.key_var.get(), [])
for word in words[:FIRST]:
self.listbox.insert(tk.END, word)
def search_word(self, *args):
query = self.search_entry.get().strip().lower()
if not query:
messagebox.showwarning("Warning", "Please enter a word to search.")
return
words = self.data.get(self.key_var.get(), [])
results = [word for word in words if query in word]
self.listbox.delete(0, tk.END)
for result in results[:FIRST]:
self.listbox.insert(tk.END, result)
def add_word(self, *args):
word = self.search_entry.get().strip().lower()
if not word:
messagebox.showwarning("Warning", "Please enter a word.")
return
if word not in self.data[self.key_var.get()]:
self.data[self.key_var.get()].append(word)
write(self.data)
self.update_listbox()
self.search_var.set('')
def delete_word(self, *args):
selected_index = self.listbox.curselection()
if not selected_index:
return
word = self.listbox.get(selected_index)
self.data[self.key_var.get()].remove(word)
write(self.data)
self.listbox.delete(selected_index)
#self.update_listbox()
if __name__ == "__main__":
root = tk.Tk()
app = JsonEditorApp(root)
root.mainloop()