Python 替换Tkinter文本小部件中的特定单词
我想知道是否有办法替换Tkinter文本小部件中特定单词的所有实例。Python 替换Tkinter文本小部件中的特定单词,python,tkinter,tkinter.text,Python,Tkinter,Tkinter.text,我想知道是否有办法替换Tkinter文本小部件中特定单词的所有实例。 例如 假设这是我的文本小部件: 你好,你好吗?天气怎么样? 我想按下一个按钮,将单词“how”的所有实例替换为单词“python” 你好,你是python吗?python是天气吗? 我可能需要使用search()函数,但我不确定接下来该怎么做 感谢您的帮助:)以下是一个工作代码: import tkinter as tk text = "Hello how are you? How is the weather?" def
例如 假设这是我的文本小部件:
你好,你好吗?天气怎么样? 我想按下一个按钮,将单词“how”的所有实例替换为单词“python”
你好,你是python吗?python是天气吗?
我可能需要使用search()函数,但我不确定接下来该怎么做
感谢您的帮助:)以下是一个工作代码:
import tkinter as tk
text = "Hello how are you? How is the weather?"
def onclick():
text_widget.delete("1.0", "end") #Deletes previous data
text_widget.insert(tk.END, text.replace("how","python").replace("How","python")) #Replacing How/how with python
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.insert(tk.END, text) #inserting the data in the text widget
text_widget.pack()
button = tk.Button(root, text = "Press me!", command = onclick)
button.pack()
root.mainloop()
输出(GIF):
您有示例代码吗?@Black Thunder没有