1

I'm making a wrong-word corrector so I use replace method, but it doesn't work because it is not all same word.

For example: string = i like icecream

I want to change the word = icecream
It only works for "i like icecream" if it is all the same

This is my whole code:

   # coding: utf-8
   from tkinter import *
   import tkinter.messagebox

   root=Tk()
   root.title("words corrector")
   root.resizable(0, 0)
   root.geometry("+{}+{}".format(600, 400))
   mainFrame = Frame(root, width=600, height=400)
   mainFrame.pack()
   textFrame = Frame(mainFrame, width=100, height=100)
   textFrame.pack()
   textFrame_1 = Frame(mainFrame, width=100, height=100)
   textFrame_1.pack()
   textFrame_2 = Frame(mainFrame,width=100, height=100)
   textFrame_2.pack()

   scrollbar = Scrollbar(textFrame)
   scrollbar.pack(side=RIGHT, fill="y")
   #textField == sentance
   textField = Text(textFrame, width=50, height=10, bd=5, relief="groove")
   textField.insert(CURRENT,"enter the text\n")
   textField.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
   textField["yscrollcommand"] = scrollbar.set

   #textField_2 == wrong word
   textField_2= Text(textFrame_1, width=15, height=3, bd=5, relief="groove")
   textField_2.insert(CURRENT,"wrong words\n")
   textField_2.pack(side=LEFT, padx=(5,0), pady=(5,5))
   #textField_3 == correct word
   textField_3= Text(textFrame_1,width=15, height=3, bd=5, relief="groove")
   textField_3.insert(CURRENT, "correct words\n")
   textField_3.pack(side=LEFT, padx=(5,0), pady=(5,5))

   def chg():
      sentance = textField.get("1.0",END)
      wrong_word = textField_2.get("1.0",END)
      correct_word = textField_3.get("1.0",END)
      result = sentance.replace(wrong_word,correct_word)
      textField_4.insert("1.0",result)

  def msg():
      tkinter.messagebox.showerror("error","there's no words")

  def ok():
      if textField_2.get("1.0",END) in textField.get("1.0",END):
          chg()

      else:
           msg()

  okButton = Button(textFrame_1, text="OK", command=ok)
  okButton.pack(padx=40, pady=(20,5))

  scrollbar_2 = Scrollbar(textFrame_2)
  scrollbar_2.pack(side=RIGHT, fill="y")
  textField_4 = Text(textFrame_2, width=50, height=10, bd=5, relief="groove")
  textField_4.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
  textField_4["yscrollcommand"] = scrollbar.set    

  root.mainloop()
kenlukas
  • 3,616
  • 9
  • 25
  • 36
haru izumi
  • 13
  • 4

1 Answers1

0

Try the following code. You have to convert unicode characters to a string and use str.replace.

from tkinter import *
import tkinter.messagebox

root=Tk()
root.title("words corrector")
root.resizable(0, 0)
root.geometry("+{}+{}".format(600, 400))
mainFrame = Frame(root, width=600, height=400)
mainFrame.pack()
textFrame = Frame(mainFrame, width=100, height=100)
textFrame.pack()
textFrame_1 = Frame(mainFrame, width=100, height=100)
textFrame_1.pack()
textFrame_2 = Frame(mainFrame,width=100, height=100)
textFrame_2.pack()

scrollbar = Scrollbar(textFrame)
scrollbar.pack(side=RIGHT, fill="y")
#textField == sentance
textField = Text(textFrame, width=50, height=10, bd=5, 
 relief="groove")
textField.insert(CURRENT,"enter the text\n")
textField.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
textField["yscrollcommand"] = scrollbar.set

#textField_2 == wrong word
textField_2= Text(textFrame_1, width=15, height=3, bd=5, 
 relief="groove")
textField_2.insert(CURRENT,"wrong words\n")
textField_2.pack(side=LEFT, padx=(5,0), pady=(5,5))
#textField_3 == correct word
textField_3= Text(textFrame_1,width=15, height=3, bd=5, 
 relief="groove")
textField_3.insert(CURRENT, "correct words\n")
textField_3.pack(side=LEFT, padx=(5,0), pady=(5,5))



scrollbar_2 = Scrollbar(textFrame_2)
scrollbar_2.pack(side=RIGHT, fill="y")
textField_4 = Text(textFrame_2, width=50, height=10, bd=5, 
 relief="groove")
textField_4.pack(side=LEFT, padx=(5, 0), pady=(5, 5))
textField_4["yscrollcommand"] = scrollbar.set    


def chg():
    sentance = textField.get("1.0",END)
    wrong_word = textField_2.get("1.0",END)
    correct_word = textField_3.get("1.0",END)
    # result = sentance.replace(wrong_word,correct_word)
    result = str.replace(str(sentance), wrong_word, correct_word)
    textField_4.insert("1.0",result)

def msg():
    tkinter.messagebox.showerror("error","there's no words")

def ok():
    # if textField_2.get("1.0",END) in textField.get("1.0",END):
    chg()

    # else:
    #      msg()
okButton = Button(textFrame_1, text="OK", command=ok)
okButton.pack(padx=40, pady=(20,5))

root.mainloop()

OUTPUT:

enter image description here

EDIT If you want want to keep that "enter the text part" and type your sentence below, without replacing it, you should do the following in the relevant line for the code to work.

result = str.replace(str(sentance).replace('enter the text\n',''), wrong_word.replace('wrong words\n',''), correct_word.replace('correct words\n','')) 
Achintha Ihalage
  • 2,310
  • 4
  • 20
  • 33
  • thank you very much but it doesn't work in my ubuntu also. I just copied your code ... what should I do? – haru izumi Aug 29 '19 at 15:39
  • I am using Ubuntu `16.04` too and this code works with both `python-2.7` and `python-3.6`. – Achintha Ihalage Aug 29 '19 at 15:41
  • oh then what's the problem? :(( – haru izumi Aug 29 '19 at 15:46
  • Btw, my `tkinter` version is `8.6`. You can check yours like this https://stackoverflow.com/questions/35999344/how-to-determine-what-version-of-python3-tkinter-is-installed-on-my-linux-machin. If it is different you may install `8.6` version and try the exact same code as above to see whether it works. – Achintha Ihalage Aug 29 '19 at 15:47
  • surprisingly, my tkinter is 8.6 too... I will upload new question why it dosen't work....anyway thank you – haru izumi Aug 29 '19 at 15:53
  • Oh I understand where your problem lies. You have to remove that 'enter the text' part from the window and type your sentence and words. – Achintha Ihalage Aug 29 '19 at 16:12
  • See the updated answer and check whether now it works. If not, you may comment here, if it does work please consider accepting the answer. – Achintha Ihalage Aug 29 '19 at 16:33
  • sorry it doesn't work also.. and I asked my friends about this , they said it does work. I don't know my problem – haru izumi Aug 30 '19 at 03:51
  • I add "print(textField_2.get("1.0",END) + ' == ' + textField.get("1.0",END)) " inside def ok() , and now it does work only 'icecream ', 'i like icecream' ,'cream' ... . could you try run this program other any words? – haru izumi Aug 30 '19 at 07:49
  • No it does not work with this `print()` function. However, the above code works very fine in my machine. – Achintha Ihalage Aug 30 '19 at 15:28
  • hmm... then if you enter "you you you" inside textfiled, is it change all "you"? mine is work only last word – haru izumi Aug 30 '19 at 16:17
  • That's because you have a newline character ('\n') at the end of this `wrong_word` and `correct_word`. When you get rid of it, the program replaces all the 'you's with `correct_word`. So you can do the following in the relevant line. `result = str.replace(str(sentance), wrong_word.strip('\n'), correct_word.strip('\n'))` – Achintha Ihalage Aug 30 '19 at 16:33