You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.2 KiB

  1. #!/usr/bin/env python3.7
  2. import tkinter
  3. import threading
  4. from ChatConnection import ChatConnection
  5. class ChatInterface(object):
  6. def __init__(self, connection: ChatConnection):
  7. # set connection to use
  8. self.connection = connection
  9. # initialise tkinter window
  10. self.window = tkinter.Tk()
  11. self.window.title("Chatter") # window title
  12. # set method to trigger when window is close
  13. self.window.protocol("WM_DELETE_WINDOW", self.close)
  14. # end of window initialisation
  15. # initialise message frame
  16. self.messagesFrame = tkinter.Frame(self.window)
  17. # # add scrollBar to see previously recovered messages
  18. self.scrollBar = tkinter.Scrollbar(self.messagesFrame)
  19. self.scrollBar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
  20. # # add messagesList to print recovered messages
  21. self.messagesList = tkinter.Listbox(
  22. self.messagesFrame, height=15, width=50, yscrollcommand=self.scrollBar.set)
  23. self.messagesList.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
  24. self.messagesList.pack()
  25. self.messagesFrame.pack()
  26. # end of message frame initialisation
  27. # initialise input
  28. self.input = tkinter.StringVar()
  29. self.input.set("Type your messages here.")
  30. # # initialise input frame
  31. self.inputFrame = tkinter.Entry(self.window, textvariable=self.input)
  32. self.inputFrame.bind("<Return>", self.send)
  33. self.inputFrame.pack()
  34. # # initialise send button
  35. self.sendButton = tkinter.Button(
  36. self.window, text="Send", command=self.send)
  37. self.sendButton.pack()
  38. # end of input initialisation
  39. def close(self, event=None) -> None:
  40. """
  41. Set input to ':quit' and trigger send method
  42. """
  43. self.input.set(":quit")
  44. self.send()
  45. def send(self, event=None) -> None:
  46. """
  47. Send message to connection
  48. """
  49. msg = self.input.get()
  50. # check if input is quit command
  51. if msg != ':quit':
  52. # clear the input bar
  53. self.input.set('')
  54. # output the client message to message list
  55. self.messagesList.insert(tkinter.END, '[YOU] {}'.format(msg))
  56. # send the message to connection
  57. self.connection.send(msg)
  58. else:
  59. # close the connection and window
  60. self.connection.close()
  61. self.window.quit()
  62. def listener(self) -> None:
  63. """
  64. Listener method to keep receiving new message from
  65. connection
  66. """
  67. while True:
  68. try:
  69. # receive messages from connection
  70. msg = self.connection.recv()
  71. # add recevied message to message list
  72. self.messagesList.insert(tkinter.END, msg[:-1])
  73. except OSError:
  74. break
  75. def start(self) -> None:
  76. """
  77. Start background listener and tkinter.mainloop
  78. """
  79. # initialise background listener as daemon thread
  80. backgroundListener = threading.Thread(target=self.listener)
  81. backgroundListener.daemon = True
  82. backgroundListener.start()
  83. # start tkinter main loop and open the GUI window
  84. tkinter.mainloop()