[英]Search for words/letters in the text widget (tkinter)
我將如何添加在文本小部件中搜索文本的搜索功能? *從用戶輸入中搜索
def openFile():
global text
artiststxt = tkinter.Tk()
artiststxt.title('Artists')
artiststxt.geometry('300x360')
artiststxt.minsize(300,360)
artiststxt.maxsize(500,360)
file = open('Artists.txt','r', encoding='utf-8')
lines = file.read()
scrollbar = Scrollbar(artiststxt, jump = 1)
text = Text(artiststxt, yscrollcommand = scrollbar.set)
scrollbar.configure(command=text.yview)
text.insert(INSERT, lines)
text.config(font=('Fixedsys', 15), fg = 'darkblue', bg = 'lightgray')
menu = tkinter.Menu(artiststxt,tearoff=0)
menu.add_command(label='Save', command = saveFile)
artiststxt.config(menu=menu)
scrollbar.pack(side=RIGHT, fill=BOTH)
text.pack()
編輯:好的,我發現了如何用這個來搜索文本:
def get(event):
global searchent
text.tag_remove('found', '1.0', END)
s = searchent.get()
if s:
idx = '1.0'
while 1:
idx = text.search(s, idx, nocase=1, stopindex=END)
if not idx: break
lastidx = '%s+%dc' % (idx, len(s))
text.tag_add('found', idx, lastidx)
idx = lastidx
text.tag_config('found', foreground='red')
searchent.focus_set()
現在,假設搜索到的文本進一步下降。 如何使滾動條向下轉到搜索到的文本?
好吧,我知道了。 花了一些時間,但值得。
首先,我們在窗口中創建一個輸入框,然后將其與Enter鍵綁定,並放置一個.get事件
searchent.bind("<Return>", get)
當按下回車鍵時,我們進入def get(event):
def get(event):
global searchent
text.tag_remove('found', '1.0', END)
s = searchent.get() # Grabs the text from the entry box
if s:
idx = '1.0'
while 1:
idx = text.search(s, idx, nocase=1, stopindex=END)
if not idx: break
lastidx = '%s+%dc' % (idx, len(s))
text.tag_add('found', idx, lastidx)
idx = lastidx
text.see(idx) # Once found, the scrollbar automatically scrolls to the text
text.tag_config('found', foreground='red')
searchent.focus_set()
我使用您的帖子Brian Fuller編寫了這段代碼,希望它對您有幫助,對我也有幫助。
from tkinter import *
from tkinter import messagebox as MessageBox
search_list = list()
s = ""
def reset_list():
if s != entry_widget_name.get():
print(entry_widget_name.get())
search_list.clear()
text_widget_name.tag_remove(SEL, 1.0,"end-1c")
def search_words():
reset_list()
global search_list
global s
text_widget_name.focus_set()
s = entry_widget_name.get()
if s:
if search_list == []:
idx = "1.0"
else:
idx = search_list[-1]
idx = text_widget_name.search(s, idx, nocase=1, stopindex=END)
lastidx = '%s+%dc' % (idx, len(s))
try:
text_widget_name.tag_remove(SEL, 1.0,lastidx)
except:
pass
try:
text_widget_name.tag_add(SEL, idx, lastidx)
counter_list = []
counter_list = str(idx).split('.')
text_widget_name.mark_set("insert", "%d.%d" % (float(int(counter_list[0])), float(int(counter_list[1]))))
text_widget_name.see(float(int(counter_list[0])))
search_list.append(lastidx)
except:
MessageBox.showinfo("Search complete","No further matches")
search_list.clear()
text_widget_name.tag_remove(SEL, 1.0,"end-1c")
root = Tk()
root.geometry("540x460")
lbl_frame_entry = LabelFrame(root, text="Enter the text to search", padx=5, pady=5)
lbl_frame_entry.pack(padx=10, pady=5, fill="both")
entry_widget_name = Entry(lbl_frame_entry, width=50, justify = "left")
entry_widget_name.pack(fill="both")
lbl_frame_text = LabelFrame(root, text="Enter the text here", padx=5, pady=5, height=260)
lbl_frame_text.pack(padx=10, pady=5, fill="both", expand=True)
text_widget_name = Text(lbl_frame_text)
text_widget_name.pack(fill="both", expand=True)
scrollbar = Scrollbar(text_widget_name, orient="vertical", command=text_widget_name.yview, cursor="arrow")
scrollbar.pack(fill="y", side="right")
text_widget_name.config(yscrollcommand=scrollbar.set)
button_name = Button(root, text="Search", command=search_words, padx=5, pady=5)
button_name.pack()
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.