Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBot-Client/index.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
220 lines (183 sloc)
8.32 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Chat client. | |
Author: Vihan, | |
Jesse Prescott, | |
Gulsum Ozer | |
""" | |
import socket | |
import time | |
from tkinter import * | |
from threading import Thread | |
Display_chat = "" | |
messageQueue = [] | |
# Jesse Prescott | |
def sendMessageToServer(text): | |
""" Send given message to server. """ | |
Display_chat.insert(END, "Client: " + text + "\n") | |
clientSocket.send(str(text).encode()) | |
#When user clicked the button on chatbot it needs to do disappear or pop up. So We made a function for it. | |
def submitButtonPressed(): | |
recive = receive_chat.get() | |
sendMessageToServer(recive) | |
user_entry.grid_remove() | |
Enter_button.grid_remove() | |
findThatContainButton.grid() | |
findThatdontContainButton.grid() | |
findThatAreFromnButton.grid() | |
ThatHaveTheLeastAmountOfCaloriesButton.grid() | |
ThatHaveTheMostAmountOfCaloriesButton.grid() | |
doneButton.grid() | |
return | |
# Jesse Prescott | |
def connectToServer(): | |
"""Attempt connection to the ChatBot server.""" | |
clientSocket = socket.socket() | |
clientSocket.connect(("127.0.0.1", 9800)) | |
thread = Thread(target = receiveLoop, args = (clientSocket, )) | |
thread.start() | |
return clientSocket | |
# Jesse prescott. | |
def receiveLoop(clientSocket): | |
while True: | |
message = clientSocket.recv(1024).decode() | |
if not message: | |
break | |
messageQueue.append(message) | |
chatbot = Tk() | |
receive_chat = StringVar() | |
chatbot.title("Chat Bot") | |
#This is the Listbox where the message will show in the screen | |
Display_chat=Text(chatbot, wrap=WORD) | |
Display_chat.grid(row=1, column=0) | |
#I created a scroll bar | |
bar= Scrollbar(chatbot) | |
bar.grid(row=2, column=6,rowspan=10) | |
#User can use this scroll bar to see older messages | |
Display_chat.configure(yscrollcommand=bar.set) | |
bar.configure(command=Display_chat.yview) | |
#This is the find Receipes For button function. | |
def findReceipesForButtonPressed(): | |
#Once user clicked one of these three button they will disappear from the screen | |
findReceipesForButton.grid_remove() | |
findVeganButton.grid_remove() | |
findVegeterianButton.grid_remove() | |
#If user clicked one of these 3 button user entry and enter button will appear on the screen | |
user_entry.grid() | |
Enter_button.grid() | |
#Once user clicked the enter button server will find this "Find receipies for message" and remove from the screen | |
sendMessageToServer("Find recipes for") | |
#Then the message will show up on the screen | |
chatbot.update() | |
#When user clicked the button it will send a message to the "findReceipesForButtonPressed" function and the find receipies for will appear on the entry | |
findReceipesForButton = Button(chatbot, command=findReceipesForButtonPressed, text="Find recipes for") | |
findReceipesForButton.grid(row=4,column=0) | |
#This is the find Receipes For button function. | |
def findVeganButtonPressed(): | |
#Once user clicked one of these three button they will disappear from the screen | |
findReceipesForButton.grid_remove() | |
findVeganButton.grid_remove() | |
findVegeterianButton.grid_remove() | |
#If user clicked one of these 3 button user entry and enter button will appear on the screen | |
user_entry.grid() | |
Enter_button.grid() | |
#Once user clicked the enter button server will find this "Find Vegan receipies for" and remove from the screen | |
sendMessageToServer("Find vegan recipes for") | |
#Then the message will show up on the screen | |
chatbot.update() | |
#When user clicked the button it will send a message to the "findVeganButtonPressed" function and the find receipies for will appear on the entry | |
findVeganButton = Button(chatbot, command=findVeganButtonPressed, text="Find vegan recipes for") | |
findVeganButton.grid(row=5,column=0) | |
def findVegeterianButtonPressed(): | |
findReceipesForButton.grid_remove() | |
findVeganButton.grid_remove() | |
findVegeterianButton.grid_remove() | |
user_entry.grid() | |
Enter_button.grid() | |
sendMessageToServer("Find vegetarian recipes for") | |
chatbot.update() | |
findVegeterianButton = Button(chatbot, command=findVegeterianButtonPressed, text="Find vegetarian recipes for") | |
findVegeterianButton.grid(row=6,column=0) | |
#Once user clicked one of the enter button 6 buttons will appear on the screen | |
#This is the find That Contain Button function. | |
def findThatContainButtonPressed(): | |
#Once user clicked find that contain button or find that don't contain button. both button will be disappear from the screen | |
findThatContainButton.grid_remove() | |
findThatdontContainButton.grid_remove() | |
#If user clicked one of these 2 buttons user entry and enter button will appear on the screen | |
user_entry.grid() | |
Enter_button.grid() | |
#Once user clicked the enter button server will find this "That contain" and remove from the screen | |
sendMessageToServer("That contain") | |
#Then the message will show up on the screen | |
chatbot.update() | |
#When user clicked the button it will send a message to the "findThatContainButtonPressed" function and the find receipies for will appear on the entry | |
findThatContainButton = Button(chatbot, command=findThatContainButtonPressed, text="That contain") | |
findThatContainButton.grid(row=5,column=0) | |
findThatContainButton.grid_remove() | |
def findThatdontContainButtonPressed(): | |
findThatContainButton.grid_remove() | |
findThatdontContainButton.grid_remove() | |
user_entry.grid() | |
Enter_button.grid() | |
sendMessageToServer("That do not contain") | |
chatbot.update() | |
findThatdontContainButton = Button(chatbot, command=findThatdontContainButtonPressed, text="That do not contain") | |
findThatdontContainButton.grid(row=6,column=0) | |
findThatdontContainButton.grid_remove() | |
#Quit button function | |
def quit(): | |
#This will send message to the server. | |
sendMessageToServer("Done.") | |
#Then chatbot will quit | |
chatbot.quit() | |
#When user clicked the button it will send a message to the "Done" function and then application will be quit. Also bg means background. so the background colour is green | |
doneButton = Button(chatbot, command=quit, text="Done", bg="green") | |
doneButton.grid(row=11,column=0) | |
doneButton.grid_remove() | |
#Gulsum Ozer# | |
def findThatAreFromnButtonPressed(): | |
findThatAreFromnButton.grid_remove() | |
user_entry.grid() | |
Enter_button.grid() | |
sendMessageToServer("That are from") | |
chatbot.update() | |
findThatAreFromnButton = Button(chatbot, command=findThatAreFromnButtonPressed, text="That are from") | |
findThatAreFromnButton.grid(row=8,column=0) | |
findThatAreFromnButton.grid_remove() | |
def ThatHaveTheLeastAmountOfCaloriesButtonPressed(): | |
ThatHaveTheLeastAmountOfCaloriesButton.grid_remove() | |
sendMessageToServer("That have least amount of calories") | |
chatbot.update() | |
ThatHaveTheLeastAmountOfCaloriesButton = Button(chatbot, command=ThatHaveTheLeastAmountOfCaloriesButtonPressed, text="That have least amount of calories") | |
ThatHaveTheLeastAmountOfCaloriesButton.grid(row=9,column=0) | |
ThatHaveTheLeastAmountOfCaloriesButton.grid_remove() | |
def ThatHaveTheMostAmountOfCaloriesButtonPressed(): | |
ThatHaveTheMostAmountOfCaloriesButton.grid_remove() | |
sendMessageToServer("That have most amount of calories") | |
chatbot.update() | |
ThatHaveTheMostAmountOfCaloriesButton = Button(chatbot, command=ThatHaveTheMostAmountOfCaloriesButtonPressed, text="That have most amount of calories") | |
ThatHaveTheMostAmountOfCaloriesButton.grid(row=10,column=0) | |
ThatHaveTheMostAmountOfCaloriesButton.grid_remove() | |
########################################################################## | |
#This is the enter where user can put anything they want | |
user_entry=Entry(chatbot,textvariable=receive_chat, width=40) | |
user_entry.grid(row=3,column=0) | |
#When user start the application this entry will not show up on the screen | |
user_entry.grid_remove() | |
#This is the enter button when user press this button it will send message to the submitButtonPressed function | |
Enter_button = Button(chatbot,text="Enter",command =submitButtonPressed, width= 5) | |
Enter_button.grid(row=3, column=1) | |
#When user start the application this button will not show up on the screen | |
Enter_button.grid_remove() | |
#This is the heading of this program. | |
label_H1=Label(chatbot, text="Welcome to the ChatBot") | |
label_H1.grid(row=0,column=0) | |
# Jesse Prescott # | |
clientSocket = connectToServer() | |
while True: | |
for message in messageQueue: | |
messageQueue.remove(message) | |
Display_chat.insert(END, "Server: " + message + "\n") | |
chatbot.update_idletasks() | |
chatbot.update() | |
################## |