How do I use the entry widget on Tkinter to execute a function by inputting parameters?

Viewed 447

I am writing code for a reddit bot that takes any given keyword, searches any given subreddit for that given key word, and if it finds that key word in the subreddit, pulls the title of the thread.

I have developed a GUI with Tkinter, and using the Entry widget, I want to be able to type in a given key word into the entry box, and have it be passed into my main bot function(run_bot) as the given key word when I activate the bot.

Now I have a button widget set up to execute a function (run_bot) which parses reddit when clicked. However, this executes the function(run_bot) with default key word and subreddit parameters that I already have written in the back end.

How do I get any key word that I type into the entry widget, connect to be the passed-in parameter in the run_bot function, and execute the function accordingly?

I hope I made sense. I can post code too if need be but I felt it wasn't necessary as I'm just trying to grasp the entry widget more.

Thanks

2 Answers

You can bind the Enter key to the entry, and have it call run_bot, with value in entry as a parameter:

something like this:

entry.bind('<Enter>', lambda: run_bot(entry.get()))

My first thought would be to use a callback every time the entry gets changed. Watch this thread for more infos: How do I get an event callback when a Tkinter Entry widget is modified?

But then you execute function would be fired by every key press, I think. If you don't want this then you probably have to use a button which triggers the execute function.

Related