-1

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

  • 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 Answers3

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()
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
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()