Merhaba arkadaşlar bu makalemizde txt dosyasından okuma yapıyoruz ve okunan içeriği Entry e aktarıyoruz. Diğer Entry e girilen stringi butona tıklayarak metin içerisinde arama yapıyoruz.
Şekil 1
Şekil 2
pyton_search_string.py
from tkinter import *
root = Tk()
root.title("text to find..bs")
frame = Frame(root)
#adding label to search box.. arama kismina label ekleniyor
Label(frame,text='Text to find (Metin icerisinde bul) :',font=("Arial,12,bold")).pack(side=LEFT)
#adding of single line text box
edit = Entry(frame,font=("Arial,12,bold"))
#positioning of text box.. textbox in konumunu belirliyoruz
edit.pack(side=LEFT, fill=BOTH, expand=1)
#setting focus..focus ayarlari yapiliyor
edit.focus_set()
#adding of search button.. arama butonu ekleniyor
btn = Button(frame, text='Search',font=("Arial,12,bold"),width=15,height=1,pady=3,fg='white', bg='deepskyblue')
btn.pack(side=RIGHT)
frame.pack(side=TOP)
#text box in root window.. ana pencerede ki text box girisi
text = Text(root,font=("Arial,12,bold"),fg='black', bg='lightyellow')
#reading a .txt and inputting contents into a textbox.. txt dosya
okunuyor ve icerik text box aktariliyor.
with open("D:\\person.txt", 'r') as passtxt:
content =
passtxt.read().strip()
if content != "":
text.insert('1.0',content)
else:
#text input
area at index 1 in text window..metin penceresinin 1. satirina asagidaki metin
girilir
text.insert('1.0','''Type your
text here..Buraya aranacak metini yaziniz..''')
text.pack(side=BOTTOM)
#function to search string in text..metin icerisindeki stringi
arama fonksiyonu
def find():
#remove tag
'found' from index 1 to END..1.indeksten son indekse bulunan etiketi
kaldirilir..
text.tag_remove('found', '1.0', END)
#returns to
widget currently in focus.. focus daki widgete donulur..
s = edit.get()
if s:
idx = '1.0'
while 1:
#searches for
desired string from index 1.. 1.indeksten itibaren istek strinh icin arama
yapilir..
idx =
text.search(s, idx, nocase=1,
stopindex=END)
if not idx: break
#last index sum
of current index and
#length of
text..metin uzunlugu ve su anki son indeks toplami..
lastidx = '%s+%dc' % (idx, len(s))
#overwrite
'Found' at idx..idx de bulunanin üzerine yazma
text.tag_add('found', idx, lastidx)
idx = lastidx
#mark located
string as red.. bulunan stringin kirmizi olarak isaretlenmesi..
text.tag_config('found', foreground='red')
edit.focus_set()
btn.config(command=find)
root.mainloop()
Click for details
Bir
makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır
ŞAHİN
0 comments:
Yorum Gönder