Question Details

No question body available.

Tags

python tkinter languagetool

Answers (2)

March 12, 2026 Score: 2 Rep: 10,624 Quality: Medium Completeness: 80%

Upon your code the issue resides upon:

for error in self.spellingerrors:
   for suggestion in error.replacements:
       self.menu.addcommand(suggestion)

The reason ehy is because there's no callback to trigger upon the action once clicked. The minimum fix you can do is:

for error in self.spellingerrors:
   for suggestion in error.replacements:
      self.menu.addcommand(label=suggestion,command=lambda s=suggestion: self.applysuggestion(s))

Then place this method:

    def applysuggestion(self, text):
        self.delete(0, tk.END)
        self.insert(0, text)
        self.spellingerrors.clear()
        self.configure(highlightbackground="gray")

This function waht is does is to overwrite the entry with suggested value.

The final script is:

import tkinter as tk
import languagetoolpython

class SpellCheckEntry(tk.Entry):

def init(self, master=None, pureenglish:bool=False, kwargs): self.var = tk.StringVar() super().init(master, textvariable=self.var, kwargs)

language = "enUS" if pureenglish else "auto" print(language) self.tool = languagetoolpython.LanguageTool(language)

self.spellingerrors=[]

self.menu = tk.Menu(self, tearoff=0) self.bind("", self.showsuggestions)

def spellcheck(self,text): matches = self.tool.check(text)

self.menu.delete(0, tk.END)

self.spellingerrors=[] for m in matches: if("ELGR" in (m.ruleid) or "ENUS" in (m.ruleid)): self.spellingerrors.append(m) print("Matching Rule",m.ruleid)

if self.spellingerrors: self.configure(highlightbackground="red", highlightcolor="red")

for error in self.spellingerrors: for suggestion in error.replacements: # FIX: Placing a callback here self.menu.addcommand( label=suggestion, command=lambda s=suggestion: self.applysuggestion(s) ) else: self.configure(highlightbackground="gray")

return self.spellingerrors

#FIX: Callback that applies data upon input def applysuggestion(self, text): self.delete(0, tk.END) self.insert(0, text) self.spellingerrors.clear() self.configure(highlightbackground="gray")

def showsuggestions(self,event): try: self.menu.tkpopup(event.xroot, event.yroot) finally: self.menu.grabrelease()

def get(self): text = super().get().strip() errors = self.spellcheck(text)

if errors: raise ValueError(f"Spelling errors detected: {text}")

return text

def submit(): try: value = entry.get() print("Valid text:", value) except ValueError as e: print(e)

root = tk.Tk() root.title("SpellCheckEntry Demo") root.geometry("400x150")

tk.Label(root, text="Type something:").pack(pady=5)

entry = SpellCheckEntry(root, pureenglish=True, width=40) entry.pack(pady=5)

btn = tk.Button(root, text="Validate", command=submit) btn.pack(pady=10)

root.mainloop()

Pay attention upon methods:

  • spellcheck
  • applysuggestion

Where I made these changes.

March 11, 2026 Score: 0 Rep: 47 Quality: Low Completeness: 60%
class SpellCheckEntry(PasteableEntry):

def init(self, master=None, pureenglish: bool = False, kwargs): self.var = tk.StringVar() super().init(master, textvariable=self.var, kwargs)

language = "en
US" if pureenglish else "auto" self.tool = languagetoolpython.LanguageTool(language) self.spellingerrors = [] self.menu = tk.Menu(self, tearoff=0) self.bind("", self.showsuggestions)

def spellcheck(self, text): matches = self.tool.check(text) self.menu.delete(0, tk.END) self.spellingerrors = []

for m in matches: if "EL
GR" in m.ruleid or "ENUS" in m.ruleid: self.spellingerrors.append(m)

if self.spellingerrors: self.configure(highlightbackground="red", highlightcolor="red") for error in self.spellingerrors: for suggestion in error.replacements[:5]: self.menu.addcommand( # ← label= keyword label=suggestion, command=lambda s=suggestion, e=error: self.applysuggestion(e, s) ) else: self.configure(highlightbackground="gray")

return self.spelling
errors

def applysuggestion(self, error, suggestion): text = self.var.get() corrected = text[:error.offset] + suggestion + text[error.offset + error.errorLength:] self.var.set(corrected) self.configure(highlightbackground="gray")

def showsuggestions(self, event): text = self.var.get().strip() self.
spellcheck(text) # ← run check before popup try: self.menu.tkpopup(event.xroot, event.yroot) finally: self.menu.grabrelease()

def get(self): text = super().get().strip() errors = self.spellcheck(text) if errors: raise ValueError(f"Spelling errors detected: {text}") return text