,

How To Create GUI Chat Bot Using Tkinter in Python

Creating a GUI chatbot using Tkinter in Python allows you to build a chat interface that users can interact with. Tkinter is a Python library for creating graphical user interfaces, and it provides tools for creating windows, buttons, text fields, and other GUI elements. By combining Tkinter with a chatbot backend, you can create a user-friendly chatbot application that users can run on their computers. This tutorial will guide you through the process of creating a simple GUI chatbot using Tkinter and Python, enabling you to learn the basics of building graphical applications while also gaining experience with chatbot development.

Let’s get started:

Create a file in your editor and save it with .py extension and paste the below code in it.

Full Code:

from tkinter import *

# GUI
root = Tk()
root.title("Chatbot")

BG_GRAY = "#ABB2B9"
BG_COLOR = "#17202A"
TEXT_COLOR = "#EAECEE"

FONT = "Helvetica 14"
FONT_BOLD = "Helvetica 13 bold"

# Send function
def send():
send = "You -> " + e.get()
txt.insert(END, "\n" + send)

user = e.get().lower()

if (user == "hello"):
txt.insert(END, "\n" + "Bot -> Hi there, how can I help?")

elif (user == "hi" or user == "hii" or user == "hiiii"):
txt.insert(END, "\n" + "Bot -> Hi there, what can I do for you?")

elif (user == "how are you"):
txt.insert(END, "\n" + "Bot -> fine! and you")

elif (user == "fine" or user == "i am good" or user == "i am doing good"):
txt.insert(END, "\n" + "Bot -> Great! how can I help you.")

elif (user == "code" or user == "i want to learn coding" or user == "can you help me with coding"):
txt.insert(END, "\n" + "Bot -> yes certainly.You can explore codemagnet !.")

elif (user == "thanks" or user == "thank you" or user == "now its my time"):
txt.insert(END, "\n" + "Bot -> My pleasure !")

elif (user == "what do you sell" or user == "what kinds of items are there" or user == "have you something"):
txt.insert(END, "\n" + "Bot -> We have coffee and tea")

elif (user == "tell me a joke" or user == "tell me something funny" or user == "crack a funny line"):
txt.insert(
END, "\n" + "Bot -> What did the buffalo say when his son left for college? Bison.! ")

elif (user == "goodbye" or user == "see you later" or user == "see yaa"):
txt.insert(END, "\n" + "Bot -> Have a nice day!")

else:
txt.insert(END, "\n" + "Bot -> Sorry! I didn't understand that")

e.delete(0, END)


lable1 = Label(root, bg=BG_COLOR, fg=TEXT_COLOR, text="Welcome", font=FONT_BOLD, pady=10, width=20, height=1).grid(
row=0)

txt = Text(root, bg=BG_COLOR, fg=TEXT_COLOR, font=FONT, width=60)
txt.grid(row=1, column=0, columnspan=2)

scrollbar = Scrollbar(txt)
scrollbar.place(relheight=1, relx=0.974)

e = Entry(root, bg="#2C3E50", fg=TEXT_COLOR, font=FONT, width=55)
e.grid(row=2, column=0)

send = Button(root, text="Send", font=FONT_BOLD, bg=BG_GRAY,
command=send).grid(row=2, column=1)

root.mainloop()

Output:

Explanation of the above code:

  1. from tkinter import *: This line imports everything (*) from the tkinter module, which is used to create graphical user interfaces (GUIs).
  2. root = Tk(): This creates the main window of the GUI, which is stored in a variable called root.
  3. root.title("Chatbot"): Sets the title of the GUI window to “Chatbot“.
  4. Constants for colors and fonts are defined:
    • BG_GRAY, BG_COLOR, TEXT_COLOR, FONT, FONT_BOLD.
  5. def send():: This line defines a function named send that will be called when the user sends a message.
  6. Inside the send() function, messages from the user are processed and appropriate responses are generated based on the input.
  7. Widgets (GUI elements) are created and configured:
    • Label for displaying a welcome message.
    • Text widget for displaying the chat history.
    • Scrollbar for scrolling the chat history.
    • Entry widget for entering messages.
    • Button for sending messages.
  8. Widgets are placed in the GUI using the grid() method, specifying their row and column positions.
  9. The send button is configured to call the send() function when clicked.
  10. root.mainloop(): This line starts the main event loop of the GUI, which listens for user inputs and events.

In conclusion, creating a GUI chatbot using Tkinter in Python is a straightforward process that allows you to build interactive applications with user-friendly interfaces. Tkinter provides a simple way to design and implement GUIs, making it suitable for beginners and experienced developers alike.

By following the steps outlined in the tutorial, you can create a functional chatbot that can respond to user inputs and provide helpful information or assistance. Tkinter’s flexibility and ease of use make it a valuable tool for developing a wide range of GUI applications, including chatbots, data visualizations, and more.

Overall, creating a GUI chatbot using Tkinter is a rewarding experience that can enhance your Python programming skills and enable you to build interactive applications that engage users in meaningful ways.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>