Let’s create a chatbot using Python and tkinter. A simple chatbot even a noob in programming can make.

Ashish Pandey
3 min readJan 16, 2023

--

Hey guys, have you ever wanted to create your own chatbot? Well, look no further because with just a few lines of Python code, you too can have your very own chatbot.

The code I have provided here is a simple chatbot using the Tkinter library in Python. Tkinter is a built-in library that allows for easy creation of graphical user interfaces (GUIs) in Python.

First, we import the Tkinter library and the datetime library. The datetime library is used to get the current time, which we will use later in the code.

Next, we define a function called “on_submit”, which is called whenever the user submits their input. In this function, we first get the user’s input and insert it into the output field with a “User:” label. We also have several “if” and “elif” statements that check the user’s input and respond accordingly. For example, if the user inputs “hi”, the chatbot will respond with “Hello, how can I help you?”. If the user inputs “time”, the chatbot will respond with the current time. If the user inputs “bye”, the chatbot will respond with “Goodbye, have a nice day!”.

The rest of the code sets up the GUI for the chatbot, including the input and output fields, labels, and buttons. The output field is also configured with different colors for the user’s input and the chatbot’s response.

Overall, this code is a great starting point for creating your own chatbot and experimenting with different responses and functionality. So go ahead and give it a try!

import tkinter as tk
import datetime

def on_submit(event=None):
user_input = input_field.get()
output_field.config(state='normal')
output_field.insert('end', 'User: ' + user_input + '\n', 'red')
if user_input == 'hi':
output_field.insert('end', 'Hello, how can I help you?\n', 'blue')
elif user_input == 'how are you':
output_field.insert('end', 'I am Okay. How about you?\n', 'blue')
elif user_input == 'how old are you':
output_field.insert('end', 'My creator just brought me to life in front of you. I am not much older than 10 minutes.\n', 'blue')
elif user_input == 'what time is it':
now = datetime.datetime.now()
output_field.insert('end', "It's "+ now.strftime("%H:%M:%S %p")+'\n', 'blue')
elif user_input == 'bye':
output_field.insert('end', 'Goodbye, have a nice day!\n', 'blue')
else:
output_field.insert('end', 'I am sorry, I do not understand.\n', 'blue')
input_field.delete(0, 'end')
output_field.config(state='disabled')

root = tk.Tk()
root.title("Chatbot")

output_frame = tk.Frame(root)
output_frame.pack(side='top', fill='both', expand=True)

output_label = tk.Label(output_frame, text="Chatbot:")
output_label.pack(side='left', padx=5, pady=5)

output_field = tk.Text(output_frame)
output_field.pack(side='left', fill='both', expand=True, padx=5, pady=5)
output_field.config(state='disabled')
output_field.tag_config('blue', foreground='blue')
output_field.tag_config('red', foreground='red')
output_field.config(font=("Futura", 16))

input_frame = tk.Frame(root)
input_frame.pack(side='bottom', fill='x')

input_label = tk.Label(input_frame, text="User:")
input_label.pack(side='left', padx=5, pady=5)

input_field = tk.Entry(input_frame, width=150)
input_field.pack(side='left', padx=5, pady=5)
input_field.bind("<Return>", on_submit)

submit_button = tk.Button(input_frame, text="Submit", command=on_submit)
submit_button.pack(side='left')

root.mainloop()

output:

chatbot using python and tkinter
Chatbot using python and tkinter

The above code will open a GUI for the chatbot, where users can input their message and receive a response from the chatbot. Feel free to experiment with different responses, and even add new functionality to the chatbot. You can also change the appearance of the GUI by playing around with the Tkinter library. And that’s it, a simple yet functional chatbot built with python and Tkinter Library.

--

--

Ashish Pandey

Kaizoku Ou ni, ore wa naru! And for now , a simple web developer