#!/usr/bin/env python3.7 import tkinter import threading from ChatConnection import ChatConnection class ChatInterface(object): def __init__(self, connection: ChatConnection): # set connection to use self.connection = connection # initialise tkinter window self.window = tkinter.Tk() self.window.title("Chatter") # window title # set method to trigger when window is close self.window.protocol("WM_DELETE_WINDOW", self.close) # end of window initialisation # initialise message frame self.messagesFrame = tkinter.Frame(self.window) # # add scrollBar to see previously recovered messages self.scrollBar = tkinter.Scrollbar(self.messagesFrame) self.scrollBar.pack(side=tkinter.RIGHT, fill=tkinter.Y) # # add messagesList to print recovered messages self.messagesList = tkinter.Listbox( self.messagesFrame, height=15, width=50, yscrollcommand=self.scrollBar.set) self.messagesList.pack(side=tkinter.LEFT, fill=tkinter.BOTH) self.messagesList.pack() self.messagesFrame.pack() # end of message frame initialisation # initialise input self.input = tkinter.StringVar() self.input.set("Type your messages here.") # # initialise input frame self.inputFrame = tkinter.Entry(self.window, textvariable=self.input) self.inputFrame.bind("", self.send) self.inputFrame.pack() # # initialise send button self.sendButton = tkinter.Button( self.window, text="Send", command=self.send) self.sendButton.pack() # end of input initialisation def close(self, event=None) -> None: """ Set input to ':quit' and trigger send method """ self.input.set(":quit") self.send() def send(self, event=None) -> None: """ Send message to connection """ msg = self.input.get() # check if input is quit command if msg != ':quit': # clear the input bar self.input.set('') # output the client message to message list self.messagesList.insert(tkinter.END, '[YOU] {}'.format(msg)) # send the message to connection self.connection.send(msg) else: # close the connection and window self.connection.close() self.window.quit() def listener(self) -> None: """ Listener method to keep receiving new message from connection """ while True: try: # receive messages from connection msg = self.connection.recv() # add recevied message to message list self.messagesList.insert(tkinter.END, msg[:-1]) except OSError: break def start(self) -> None: """ Start background listener and tkinter.mainloop """ # initialise background listener as daemon thread backgroundListener = threading.Thread(target=self.listener) backgroundListener.daemon = True backgroundListener.start() # start tkinter main loop and open the GUI window tkinter.mainloop()