gpt4 book ai didi

python - 如何替换 tkinter 中的文本?

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:41 25 4
gpt4 key购买 nike

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

例如: 字符串=我喜欢冰淇淋

我想更改这个词=冰淇淋
它只适用于“我喜欢冰淇淋”,​​如果它都是一样的

这是我的整个代码:

   # 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com