python - 如何替换 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.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 字符转换为 string 并使用 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()

输出:

enter image description here

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

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

关于python - 如何替换 tkinter 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712796/

相关文章:

python - Tkinter 和绑定(bind)到 ListboxSelect 事件的两个列表框出现意外行为

python - CUDA GPU处理: TypeError: compile_kernel() got an unexpected keyword argument 'boundscheck'

python - 递归单元测试发现

Python简单 turtle 程序

python - 如何使用Tkinter绘制菱形?

python - tkinter 中的父/母与 in_

python - python排序方法在循环内的时间复杂度

python - 如何获得 tkinter 按钮的大小?

python - 编译多线程Python受GIL影响

python - Numba python3 出错 [GPU ufunc 要求数组参数具有准确的类型。]