Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Autocomplete ComboBox in Python-Tkinter
Next article icon

Tkinter is the standard GUI library for Python. It provides a powerful object-oriented interface to the Tk GUI toolkit. In this article, we'll see how to search for a specific string in a given text window using Tkinter.
NOTE : For more detailed information onTkinter, refer to
Python GUI - Ttkinter
Method to create user-defined function to search(def find) 
An inner loop will search the text widget for all instances of each word, tagging them for highlighting. The termination condition for the loop is that the current word was not found in the widget. Then, after all, words have been tagged, set their color. 
 

  • Declare a variable s and get the string input of the user to be searched in the text( in this case the string is typed in 'edit' text box window) 
     
  • Initialize index value as 1. 
     
  • Initialize inner loop. 
     
  • Using .search() search for the desired string (in this case s) in the given text and update the current index value till the end of the text. 
     
  • Last Index value is the addition of the current index and length of the string. 
     
  • Add 'found' tag from index 1 to last index whenever a desired string is found in between. 
     
  • Change focus to find button 
     
  • Once find button is pressed. the string with 'found' tags, the string is highlighted in red. 
     
  • use .mainloop() for termination as any user will terminate the program 
     


Example 1: 
 

Python3
#Python Program to search string in text using Tkinterfromtkinterimport*#to create a windowroot=Tk()#root window is the parent windowfram=Frame(root)#adding label to search boxLabel(fram,text='Text to find:').pack(side=LEFT)#adding of single line text boxedit=Entry(fram)#positioning of text boxedit.pack(side=LEFT,fill=BOTH,expand=1)#setting focusedit.focus_set()#adding of search buttonbutt=Button(fram,text='Find')butt.pack(side=RIGHT)fram.pack(side=TOP)#text box in root windowtext=Text(root)#text input area at index 1 in text windowtext.insert('1.0','''Type your text here''')text.pack(side=BOTTOM)#function to search string in textdeffind():#remove tag 'found' from index 1 to ENDtext.tag_remove('found','1.0',END)#returns to widget currently in focuss=edit.get()ifs:idx='1.0'while1:#searches for desired string from index 1idx=text.search(s,idx,nocase=1,stopindex=END)ifnotidx:break#last index sum of current index and#length of textlastidx='%s+%dc'%(idx,len(s))#overwrite 'Found' at idxtext.tag_add('found',idx,lastidx)idx=lastidx#mark located string as redtext.tag_config('found',foreground='red')edit.focus_set()butt.config(command=find)#mainloop function calls the endless loop of the window,#so the window will wait for any#user interaction till we close itroot.mainloop()

OUTPUT : 

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20200327124102/tkinter2.png" alt="Search String in Text using Python-Tkinter
">


The larger text box is for the text input and the smaller text box is for the string input that needs to be found in the given text and once found, it is marked in red.
Example 2: 
 

Python3
#Python Program to search string in text using Tkinterfromtkinterimport*root=Tk()fram=Frame(root)Label(fram,text='Text to find:').pack(side=LEFT)edit=Entry(fram)edit.pack(side=LEFT,fill=BOTH,expand=1)edit.focus_set()butt=Button(fram,text='Find')butt.pack(side=RIGHT)fram.pack(side=TOP)text=Text(root)text.insert('1.0','''Type your text here''')text.pack(side=BOTTOM)deffind():text.tag_remove('found','1.0',END)s=edit.get()ifs:idx='1.0'while1:idx=text.search(s,idx,nocase=1,stopindex=END)ifnotidx:breaklastidx='%s+%dc'%(idx,len(s))text.tag_add('found',idx,lastidx)idx=lastidxtext.tag_config('found',foreground='red')edit.focus_set()butt.config(command=find)root.mainloop()

OUTPUT : 

<img src="https://media.geeksforgeeks.org/wp-content/uploads/20200327124532/tkinter21.png" alt="Search String in Text using Python-Tkinter
">


 


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp