|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import filedialog, messagebox |
| 3 | + |
| 4 | +class AutocompleteApp: |
| 5 | + def __init__(self, root): |
| 6 | + self.root = root |
| 7 | + self.root.title("Autocomplete Notes App") |
| 8 | + |
| 9 | + # Load words from wordlist.txt |
| 10 | + self.autocomplete_list = self.load_wordlist("wordlist.txt") |
| 11 | + |
| 12 | + self.create_widgets() |
| 13 | + |
| 14 | + def load_wordlist(self, filename): |
| 15 | + """Load words from a specified file.""" |
| 16 | + try: |
| 17 | + with open(filename, 'r') as file: |
| 18 | + words = [line.strip() for line in file.readlines() if line.strip()] |
| 19 | + return words |
| 20 | + except FileNotFoundError: |
| 21 | + messagebox.showerror("Error", f"File '{filename}' not found.") |
| 22 | + return [] |
| 23 | + |
| 24 | + def create_widgets(self): |
| 25 | + self.large_entry = tk.Text(self.root, wrap=tk.WORD) |
| 26 | + self.large_entry.grid(row=0, column=0, padx=10, pady=10, sticky="nsew") |
| 27 | + |
| 28 | + self.suggestion_entry = tk.Entry(self.root, width=60, state='readonly') |
| 29 | + self.suggestion_entry.grid(row=1, column=0, padx=10, pady=5, sticky="ew") |
| 30 | + |
| 31 | + self.save_button = tk.Button(self.root, text="Save Notes", command=self.save_notes) |
| 32 | + self.save_button.grid(row=2, column=0, padx=10, pady=5, sticky="ew") |
| 33 | + |
| 34 | + self.root.grid_rowconfigure(0, weight=1) |
| 35 | + self.root.grid_columnconfigure(0, weight=1) |
| 36 | + |
| 37 | + self.large_entry.bind("<KeyRelease>", self.update_suggestion) |
| 38 | + self.large_entry.bind("<Tab>", self.insert_complete_word) |
| 39 | + |
| 40 | + def update_suggestion(self, event): |
| 41 | + """Update the suggestion based on the current input.""" |
| 42 | + current_input = self.get_last_word() |
| 43 | + suggestion = "" |
| 44 | + |
| 45 | + if current_input: |
| 46 | + matches = [word for word in self.autocomplete_list if word.startswith(current_input)] |
| 47 | + if matches: |
| 48 | + suggestion = matches[0] |
| 49 | + |
| 50 | + self.suggestion_entry.config(state='normal') |
| 51 | + self.suggestion_entry.delete(0, tk.END) |
| 52 | + self.suggestion_entry.insert(0, suggestion) |
| 53 | + self.suggestion_entry.config(state='readonly') |
| 54 | + |
| 55 | + def get_last_word(self): |
| 56 | + """Get the last word from the current line of the large entry.""" |
| 57 | + cursor_index = self.large_entry.index(tk.INSERT) |
| 58 | + line_number = cursor_index.split('.')[0] |
| 59 | + line_text = self.large_entry.get(f"{line_number}.0", f"{line_number}.end").strip() # Get the current line text |
| 60 | + words = line_text.split() |
| 61 | + return words[-1] if words else "" |
| 62 | + |
| 63 | + def insert_complete_word(self, event): |
| 64 | + """Insert the complete word into the large entry when Tab is pressed.""" |
| 65 | + complete_word = self.suggestion_entry.get() |
| 66 | + if complete_word: |
| 67 | + cursor_index = self.large_entry.index(tk.INSERT) |
| 68 | + line_number = cursor_index.split('.')[0] |
| 69 | + line_text = self.large_entry.get(f"{line_number}.0", f"{line_number}.end").strip() |
| 70 | + words = line_text.split() |
| 71 | + if words: |
| 72 | + words[-1] = complete_word |
| 73 | + self.large_entry.delete(f"{line_number}.0", f"{line_number}.end") |
| 74 | + self.large_entry.insert(f"{line_number}.0", ' '.join(words)) |
| 75 | + |
| 76 | + return "break" |
| 77 | + |
| 78 | + def save_notes(self): |
| 79 | + """Save the notes to a file.""" |
| 80 | + notes = self.large_entry.get("1.0", tk.END).strip() |
| 81 | + if notes: |
| 82 | + file_path = filedialog.asksaveasfilename(defaultextension=".txt", |
| 83 | + filetypes=[("Text files", "*.txt"), |
| 84 | + ("All files", "*.*")]) |
| 85 | + if file_path: |
| 86 | + with open(file_path, 'w') as file: |
| 87 | + file.write(notes) |
| 88 | + messagebox.showinfo("Success", "Notes saved successfully!") |
| 89 | + else: |
| 90 | + messagebox.showwarning("Warning", "No notes to save!") |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + root = tk.Tk() |
| 94 | + app = AutocompleteApp(root) |
| 95 | + root.mainloop() |
0 commit comments