📜  在 Tkinter 文本小部件中创建查找和替换功能

📅  最后修改于: 2022-05-13 01:54:51.457000             🧑  作者: Mango

在 Tkinter 文本小部件中创建查找和替换功能

先决条件: Python GUI – tkinter
首先,我们需要在下面的文本中找到要替换的所有单词或字母,为此我们将使用 find函数,在该函数中,我们将从同一单词的编辑器中确定所有开始和结束索引,之后我们将应用替换功能,我们将删除该部分文本,之后,我们将应用插入函数在该确切位置添加文本。
使用的重要内置函数

  • Tk()它创建一个基本窗口/tkinter 基本小部件
  • Frame()它在 Tk() 实例的特定位置创建一个单独的 Frame
  • Label()它添加任何东西的语句或名称或标签
  • Entry()它添加了输入文本的对话框
  • 'EntryInstance' .pack()它在指定位置打包输入框
  • Button()它用命令和标签在指定位置放置一个按钮
  • Text()它放置一个文本框来编写和添加内容
  • 'TextInstance' .insert()它在指定索引处添加文本(此处索引为字符串类型)
  • 'TextInstance' .tag_add()标签用于同时引用所有内容,例如,如果一个段落包含五次文本“this”并且为了同时对它们进行更改,我们使用标签. tag_Add 将所有这些文本添加到我们提供名称的特定标签下
  • 'TextInstance' .tag_config()它们用于配置它们是高亮、字体、背景色、前景色
  • 'TextInstance' .tag_remove()用于删除从起始索引到结束索引的所有文本
  • 'EntryInstance' .get()用于访问 Entry 对话框中输入的文本
  • 'TextInstance' .search()用于在整个编辑器中搜索特定文本,从开始索引到结束,参数为 'nocase',如果设置为 1,则在搜索中不考虑区分大小写
  • 'ButtonInstance' .config()用于配置按钮,我们可以单独添加命令或按钮按下时将发生的更改
  • 'TkInstance' .mainloop()用于确保文本小部件保持打开状态

下面是实现。

Python3
from tkinter import *
 
 
# to create a window
root = Tk()
 
# root window is the parent window
fram = Frame(root)
 
# Creating Label, Entry Box, Button
# and packing them adding label to
# search box
Label(fram, text ='Find').pack(side = LEFT)
 
# adding of single line text box
edit = Entry(fram)
 
# positioning of text box
edit.pack(side = LEFT, fill = BOTH, expand = 1)
 
# setting focus
edit.focus_set()
 
# adding of search button
Find = Button(fram, text ='Find')
Find.pack(side = LEFT)
 
 
Label(fram, text = "Replace With ").pack(side = LEFT)
 
edit2 = Entry(fram)
edit2.pack(side = LEFT, fill = BOTH, expand = 1)
edit2.focus_set()
 
replace = Button(fram, text = 'FindNReplace')
replace.pack(side = LEFT)
 
fram.pack(side = TOP)
 
# text box in root window
text = Text(root)
 
# text input area at index 1 in text window
text.insert('1.0', '''Type your text here''')
text.pack(side = BOTTOM)
 
# function to search string in text
def find():
     
    # remove tag 'found' from index 1 to END
    text.tag_remove('found', '1.0', END)
     
    # returns to widget currently in focus
    s = edit.get()
     
    if (s):
        idx = '1.0'
        while 1:
            # searches for desired string from index 1
            idx = text.search(s, idx, nocase = 1,
                            stopindex = END)
             
            if not idx: break
             
            # last index sum of current index and
            # length of text
            lastidx = '% s+% dc' % (idx, len(s))
             
 
            # overwrite 'Found' at idx
            text.tag_add('found', idx, lastidx)
            idx = lastidx
 
        # mark located string as red
         
        text.tag_config('found', foreground ='red')
    edit.focus_set()
 
def findNreplace():
     
    # remove tag 'found' from index 1 to END
    text.tag_remove('found', '1.0', END)
     
    # returns to widget currently in focus
    s = edit.get()
    r = edit2.get()
     
    if (s and r):
        idx = '1.0'
        while 1:
            # searches for desired string from index 1
            idx = text.search(s, idx, nocase = 1,
                            stopindex = END)
            print(idx)
            if not idx: break
             
            # last index sum of current index and
            # length of text
            lastidx = '% s+% dc' % (idx, len(s))
 
            text.delete(idx, lastidx)
            text.insert(idx, r)
 
            lastidx = '% s+% dc' % (idx, len(r))
             
            # overwrite 'Found' at idx
            text.tag_add('found', idx, lastidx)
            idx = lastidx
 
        # mark located string as red
        text.tag_config('found', foreground ='green', background = 'yellow')
    edit.focus_set()
 
                 
Find.config(command = find)
replace.config(command = findNreplace)
 
# mainloop function calls the endless
# loop of the window, so the window will
# wait for any user interaction till we
# close it
root.mainloop()


输出: