def new_window(self): #bot init see ChatBot self.bot = ChatBot() #Gui initi see ChatGUI self.gui = ChatGUI(self.master, self.bot) #binds the enter button for the user input to the enter() method #when enter is pressed the enter method is called button = self.gui.enterButton self.master.bind('<Return>', (lambda event: self.enter())) #welcome msg from the system self.gui.updateChat("Welcome to the chatbot 4000 \n") self.gui.updateChatBot("Hello there! My name is " + self.bot.botName + ". How can I help you today?")
def __init__(self, serverPort): self.app = QtGui.QApplication(sys.argv) super(ChatClient, self).__init__() self.serverPort = serverPort self.GUI = ChatGUI() self.GUI.connectMessageInput(self.send_message) self.clientThread = thread.start_new_thread(self.run, ()) # Connects relevant signals to GUI slots self.connect(self, QtCore.SIGNAL("updateUsers"), self.GUI.setUserList) self.connect(self, QtCore.SIGNAL("addMessage"), self.GUI.addMessage) self.connect(self, QtCore.SIGNAL("exit"), self.GUI.exit) self.app.exec_()
class Chat: #init the master class def __init__(self, master): self.master = master self.new_window() #builds and initializes the gui and bot def new_window(self): #bot init see ChatBot self.bot = ChatBot() #Gui initi see ChatGUI self.gui = ChatGUI(self.master, self.bot) #binds the enter button for the user input to the enter() method #when enter is pressed the enter method is called button = self.gui.enterButton self.master.bind('<Return>', (lambda event: self.enter())) #welcome msg from the system self.gui.updateChat("Welcome to the chatbot 4000 \n") self.gui.updateChatBot("Hello there! My name is " + self.bot.botName + ". How can I help you today?") ''' Connects both ChatGui and ChatBot when the user inputs some text in the gui, it is sent to the bot via the GUI. This is so that the user can use both the enter button on the gui and the keyboard ''' def enter(self): self.gui.enter_command()
def userRefresher(): while True: Requests.listUsers() time.sleep(2.5) def messageSentTo(toNickName, text): global usersToId myId = usersToId[nickname] toId = usersToId[toNickName] Requests.sendTo(myId, toId, text) Requests.register(nickname) chatGui = gui.ChatGUI(600, 600, nickname, messageSentTo) def availableUserHandler(message): global usersToId global nickname usersToId = copy.deepcopy(message) usersList = list(usersToId.keys()) usersList.remove(nickname) if usersList: chatGui.updateAvailableUsers(usersList) def newMessageFrom(fromId, text): global usersToId for user, userId in usersToId.items():
class ChatClient(QtCore.QObject): userName = "" # Constructor # def __init__(self, serverPort): self.app = QtGui.QApplication(sys.argv) super(ChatClient, self).__init__() self.serverPort = serverPort self.GUI = ChatGUI() self.GUI.connectMessageInput(self.send_message) self.clientThread = thread.start_new_thread(self.run, ()) # Connects relevant signals to GUI slots self.connect(self, QtCore.SIGNAL("updateUsers"), self.GUI.setUserList) self.connect(self, QtCore.SIGNAL("addMessage"), self.GUI.addMessage) self.connect(self, QtCore.SIGNAL("exit"), self.GUI.exit) self.app.exec_() # process_command - Takes expected command line arguments to initialize username. # def process_command(self,command): if not command: print "Error, username not provided. Program terminated" sys.exit() else: self.userName = command # send_message - Obtains the text from chat input and sends them to server. # PUT is prepended to indicate the method. # def send_message(self): message = self.GUI.chatInput.text().toAscii() if message: message = "PUT " + message self.GUI.chatInput.clear() self.serverSocket.send(message) self.GUI.chatInput.setFocus() # get_data - Polls the server for the most recent users list and the most # recent chat log. The specific functionality of each is delegated # to an appropriate function call. # def get_data(self): while(1): time.sleep(.25) self.get_users() self.get_msgs() # get_users - Polls the server for the most recent list of users. This # list is sent to the GUI. # def get_users(self): message = "GETUSERS" try: self.serverSocket.send(message) chats = self.serverSocket.recv(4196) chatsArray = chats.split(" ",1) if chatsArray[0] == "USERS": self.emit(QtCore.SIGNAL("updateUsers"), chatsArray[1]) except Exception, e: print "Error connecting to server" self.emit(QtCore.SIGNAL("exit"),) sys.exit(0)