Make A Very Simple ChatBot With Python And Tkinter
Hello Friends...
Tonight we're going to make a very simple chatbot in Python 2 and with Tkinter as its GUI
ok...let's get started
what you need are python 2, text editor, and terminal/command prompt, and a mineral water to refreshing ur brain ^_^
Open your text editor, follow the instruction below
At the first, we need to import all module we need :
from Tkinter import *
from random import choice
And then we make some lists for user question and bot answer
ask = ["hi", "hello"]
hi = ["hi", "hello", "Hello too"]
error = ["what?", "what u said?" ]
ok...Here is the GUI program :
root = Tk()
user = StringVar()
bot = StringVar()
Label(root, text=" user : ").pack(side=LEFT)
Entry(root, textvariable=user).pack(side=LEFT)
Label(root, text=" Bot : ").pack(side=LEFT)
Entry(root, textvariable=bot).pack(side=LEFT)
mainloop()
The display is like this below :
It's look nice, but it doesn't work yet. because we have to make the main program to process and a Button to run it...
ok, here we go
main program :
def main():
question = user.get()
if question in ask:
bot.set(choice(hi))
else:
bot.set(choice(error))
And here's the button to display the answer :
Button(root, text="speak", command=main).pack(side=LEFT)
save these scripts as a .py file [ex: chatbot.py] and run it...
Nice, we have made our own simple chatbot, modify these scripts to know more...
make it better ... ^_^
<<< It's the complete program >>>
from Tkinter import *
from random import choice
ask = ["hi", "hello"]
hi = ["hi", "hello", "Hello too"]
error = ["sorry, i don't know", "what u said?" ]
root = Tk()
user = StringVar()
bot = StringVar()
root.title(" Simple ChatBot ")
Label(root, text=" user : ").pack(side=LEFT)
Entry(root, textvariable=user).pack(side=LEFT)
Label(root, text=" Bot : ").pack(side=LEFT)
Entry(root, textvariable=bot).pack(side=LEFT)
def main():
question = user.get()
if question in ask:
bot.set(choice(hi))
else:
bot.set(choice(error))
Button(root, text="speak", command=main).pack(side=LEFT)
mainloop()
some references for you :
https://www.python.org/
https://wiki.python.org/moin/TkInter
http://infohost.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
ok guys... thanks for reading. Don't forget to study and enjoy ur life everyday ^_^
Sorry for my poor english and feel free to ask ....