Having trouble using tkinter in python to create a simple gui for my simple chatbot Having trouble using tkinter in python to create a simple gui for my simple chatbot tkinter tkinter

Having trouble using tkinter in python to create a simple gui for my simple chatbot


There's a lot of room for improvement in this code, but this should get you heading in the right direction.

It's a common convention in well-written Tkinter code to do import tkinter as tk. That lets us write tk instead of tkinter, which saves typing, and makes it easy to change the code to run on Python 2 if we need to do that: just change the import statement to import Tkinter as tk. And it's much better than doing from tkinter import *, because it lets us see what names we've imported from Tkinter, and it doesn't dump over a hundred names into our namespace.

As PRMoureu mentioned in the comments, we don't want a while loop in the callback function: we just need to respond to the string that's in the Entry widget when the user presses the button.

We create a single Label widget to hold the bot's response text, and we can update it with the Labels.config` method.

import randomimport tkinter as tkroot = tk.Tk()user_input = tk.Entry(root)user_input.pack()greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!', 'hey']question = ['How are you?', 'How are you doing?']responses = ['Okay', "I'm fine"]huh = "I did not understand what you said"def cb():    user_text = user_input.get()    if user_text in greetings:        bot_text = random.choice(greetings)    elif user_text in question:        bot_text = random.choice(responses)    else:        bot_text = huh    output.config(text=bot_text)button = tk.Button(root, text="Enter", command=cb)button.pack()output = tk.Label(root, text='')output.pack()tk.mainloop()

It takes a little while to get used to GUI programming, because the control flow is not what you're used to from console programming. With code that runs in the console, the code does what it wants to do, when it wants to do it, and the user responds. In GUI code, we set everything up and then wait for events generated by the user's actions, and then our code responds to those actions. This is called event-driven programming. Although it can be a bit disorienting at first, with practice you'll soon get the hang of it. ;)


We don't really need that button. Instead, we can bind our callback to the Entry widget so that it's called when the Enter key is pressed inside the Entry widget. We have to modify the signature of the callback because it will now receive an Event object when it's called. But we don't need to change anything inside the callback function body because we aren't actually using the data in the Event object.

Here's the new version:

import randomimport tkinter as tkroot = tk.Tk()user_input = tk.Entry(root)user_input.pack()greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!', 'hey']question = ['How are you?', 'How are you doing?']responses = ['Okay', "I'm fine"]huh = "I did not understand what you said"def cb(event):    user_text = user_input.get()    if user_text in greetings:        bot_text = random.choice(greetings)    elif user_text in question:        bot_text = random.choice(responses)    else:        bot_text = huh    output.config(text=bot_text)user_input.bind("<Return>", cb)output = tk.Label(root, text='')output.pack()tk.mainloop()