Python 如何替换tkinter中的文本?

Python 如何替换tkinter中的文本?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我做了一个错误的单词校正器,所以我使用了替换方法,但它不起作用 因为它不全是同一个词 例如: 我喜欢冰淇淋 我想更改单词=冰淇淋 它只适用于“我喜欢冰淇淋”如果它是一样的 这是我的全部代码: # coding: utf-8 from tkinter import * import tkinter.messagebox root=Tk() root.title("words corrector") root.resizable(0, 0) root.ge

我做了一个错误的单词校正器,所以我使用了替换方法,但它不起作用 因为它不全是同一个词

例如: 我喜欢冰淇淋

我想更改单词=
冰淇淋

它只适用于“我喜欢冰淇淋”如果它是一样的

这是我的全部代码:

   # 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()

请尝试以下代码。您必须将unicode字符转换为
字符串
,并使用
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()
输出:

编辑 如果您希望保留“输入文本部分”并在下面键入您的句子,而不替换它,则应在相关行中执行以下操作,以使代码正常工作

result = str.replace(str(sentance).replace('enter the text\n',''), wrong_word.replace('wrong words\n',''), correct_word.replace('correct words\n','')) 

请尝试以下代码。您必须将unicode字符转换为
字符串
,并使用
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()
输出:

编辑 如果您希望保留“输入文本部分”并在下面键入您的句子,而不替换它,则应在相关行中执行以下操作,以使代码正常工作

result = str.replace(str(sentance).replace('enter the text\n',''), wrong_word.replace('wrong words\n',''), correct_word.replace('correct words\n','')) 

请发布没有行号的代码我很抱歉我刚刚编辑过请发布没有行号的代码我很抱歉我刚刚编辑过非常感谢,但它在我的ubuntu中也不起作用。我刚刚复制了你的代码。。。我该怎么办?我也在使用Ubuntu
16.04
,这段代码同时适用于
python-2.7
python-3.6
。哦,那有什么问题吗((顺便说一句,我的
tkinter
版本是
8.6
。你可以这样检查你的版本。如果它不同,你可以安装
8.6
版本,并尝试与上面完全相同的代码,看看它是否工作。令人惊讶的是,我的tkinter也是8.6…我将上传新的问题为什么它不工作…无论如何,非常感谢你,但它确实工作了我的ubuntu也不工作。我只是复制了你的代码…我该怎么办?我也在使用ubuntu
16.04
,这段代码同时适用于
python-2.7
python-3.6
。哦,那有什么问题吗((顺便说一句,我的
tkinter
版本是
8.6
。你可以这样检查你的版本。如果它不同,你可以安装
8.6
版本,并尝试与上面完全相同的代码,看看它是否工作。令人惊讶的是,我的tkinter也是8.6…我将上传新问题为什么它不工作…无论如何,谢谢你。)