I use a tkinter for entry and search by entry boxes, is there a code i can write it to give me a message if the entry box contain a specific word (for example: bachelor) Thanks
Asked
Active
Viewed 887 times
-1
-
Umm.. What have you tried so far? Also you can use a ```StringVar()``` to get to your goal – Jun 07 '21 at 12:53
-
Sorry, I can't understand how i use stringvar() for that – Ahmed Mohammed Mohammed Morgan Jun 07 '21 at 13:06
-
It's not clear what part of the problem you need help with. Do you know how to get data out of an entry widget? Do you know how to check if a sequence of characters is in a string? Do you know how to do pattern matching within a string? Something else? – Bryan Oakley Jun 07 '21 at 13:27
3 Answers
0
from tkinter import *
from tkinter import messagebox
root = Tk()
def check_entry():
v = ent.get()
if v == "hello world":
print("hello world")
else:
messagebox.showerror("it is not hello world")
print("write hello world")
ent.delete(0,END)
ent = Entry(root)
button = Button(root,text="Click me",command=check_entry)
ent.pack()
button.pack()
root.mainloop()

thilina perera
- 7
- 4
-
Thank you , but i want if the entry contain the word as a part of text , in sqlite i use like '%' + 'the word' +'%' , can i use like that with entry? – Ahmed Mohammed Mohammed Morgan Jun 07 '21 at 13:17
-
in the if statement you can write something like if v in "hello world": it will check whether the text "hello world" is in the entry.get() result. – thilina perera Jun 11 '21 at 11:53
0
@Sujay was telling you to use something like this:
import tkinter as tk
from tkinter.messagebox import showinfo
def check_word_in_entry(*args):
user_input = entry.get()
# If you don't want casing to matter, change this to:
# if "hi" in user_input.lower():
if "hi" in user_input:
# print("The word \"hi\" is inside the entry.")
showinfo(message="The word \"hi\" is inside the entry.")
root = tk.Tk()
root.geometry("400x400")
variable = tk.StringVar(root)
variable.trace("w", check_word_in_entry)
entry = tk.Entry(root, textvar=variable)
entry.pack()
root.mainloop()
It calls check_word_in_entry
each time the text inside the entry is changed. And it checks if "hi"
is inside the entry's text.

TheLizzard
- 7,248
- 2
- 11
- 31
-
This doesn't so much search for a _word_ as it does a sequence of characters. If you search for "hi", your code will match the word "hi" but also the word "this". It's not entirely clear if that matters to the OP. – Bryan Oakley Jun 07 '21 at 13:29
-
@AhmedMohammedMohammedMorgan Can you please tell me if searching for the word `"hi"` should match things like: `"this"` because it has `hi` inside there? – TheLizzard Jun 07 '21 at 13:34
-
-
@AhmedMohammedMohammedMorgan Just look [here](https://docs.python.org/3/library/tkinter.messagebox.html) – TheLizzard Jun 07 '21 at 13:38
-
-
0
from tkinter import *
root = Tk()
ent = Entry(root)
ent.pack()
def task():
print("hello world" in ent.get())
root.after(1, task) # reschedule event in 2 seconds
root.after(1, task)
root.mainloop()

thilina perera
- 7
- 4
-
You are rescheduling every 1 millisecond :D and that can take up a lot of computational power. – TheLizzard Jun 07 '21 at 13:33
-
Thank you but sorry how can i get a messagebox as a result for that – Ahmed Mohammed Mohammed Morgan Jun 07 '21 at 13:35
-
@TheLizzard yeah it will make it one 1 millisecond so that it will work as fast as possible without any delay in the programme – thilina perera Jun 11 '21 at 11:57
-
@thilinaperera Yes but the user wouldn't be able to notice a 100 millisecond (0.1 sec) delay. Run the program and check your CPU usage. Then try it with a `100` millisecond delay. – TheLizzard Jun 11 '21 at 12:04
-