Mohab Mostafa
Mohab Mostafa

Reputation: 119

Getting value from entry Tkinter widget for search in Arabic

I am trying to make a search app which search for text in HTML files using BeautifulSoup to parse HTML files and get search keyword from Tkinter entry widget to search with. I used AwesomeTkinter to display Arabic correctly inside the Tkinter Entry widget. Text displays fine and the value is correct but I get no result, while entering the same value manually inside the code works fine. can anyone help me with that please?

Here is the code I used

def searchFiles():
    global searchFolderPath
    count = 0
    searched_word = searchField.get()
    print(searched_word)
    if not searched_word:
        print(searched_word)
        searchTree.insert(parent='', index='end', iid=count, text="Please enter a search keyword first...")
    else:
        _, _, filenames_search = next(walk(searchFolderPath))
        results = {}
        for file in filenames_search:
            if file.split('.')[1] == 'html':
                soup = bs4.BeautifulSoup(open(f'{searchFolderPath}/{file}', encoding="utf-8"), 'html.parser')
                results = soup.body.find_all(string=re.compile('.*{0}.*'.format(searched_word)), recursive=True)
                if results:
                    searchTree.insert(parent='', index='end', iid=count, text=file)
                    count += 1
        if not results:
            searchTree.insert(parent='', index='end', iid=count, text="No Result")

and this is what I used to make Arabic letters display correctly

import tkinter as tk    
import awesometkinter as atk

searchField = tk.Entry(tab3, justify='right', width=50)
searchField.pack()
atk.add_bidi_support(searchField)

Upvotes: 0

Views: 171

Answers (3)

Stephen Palincsar
Stephen Palincsar

Reputation: 1

filenames_search = next(walk(searchFolderPath))

results = {""}


for file in filenames_search:


    if file.split('.')[1] == 'html':


        soup = bs4.BeautifulSoup(open(f'{searchFolderPath}/{file}', encoding="utf-8"), 'html.parser')


        results = soup.body.find_all(string=re.compile('.*{0}.*'.format(searched_word)), recursive=True)


        if results:



            searchTree.insert(parent='', index='end', iid=count, text=file)
            

            count += 1

you are just typing code frequently just remember about the lines which is causing error try mine i managed code in a order!!

Upvotes: 0

Derek
Derek

Reputation: 2244

In order to retrieve text from Entry object try inserting the following code snippet.

I've renamed objects to conform to your code example.

def getmyentry( ev ):
    print( var3.get() )

var3 = tk.StringVar()
searchField = tk.Entry(tab3, justify='right', width=50, textvariable = var3) 
searchField.pack()
bound = searchField.bind( "<Return>", getmyentry )

You can now enter text into your entry box and press Return key to retrieve it

Upvotes: 0

gfdsweds
gfdsweds

Reputation: 353

Getting value from entry Tkinter widget

Use searchField.get() to return the value in the entry widget

Upvotes: 0

Related Questions