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.

106 lines
2.8 KiB

4 years ago
  1. #!/usr/bin/env python3
  2. """Script for Tkinter GUI chat client."""
  3. from socket import AF_INET, socket, SOCK_STREAM
  4. from threading import Thread
  5. import tkinter
  6. def receive():
  7. """Handles receiving of messages."""
  8. while True:
  9. try:
  10. msg = client_socket.recv(BUFSIZ).decode("utf8")
  11. msg_list.insert(tkinter.END, msg)
  12. except OSError: # Possibly client has left the chat.
  13. break
  14. def send(event=None): # event is passed by binders.
  15. """Handles sending of messages."""
  16. msg = my_msg.get()
  17. my_msg.set("") # Clears input field.
  18. client_socket.send(bytes(msg, "utf8"))
  19. if msg == "{quit}":
  20. client_socket.close()
  21. top.quit()
  22. def on_closing(event=None):
  23. """This function is to be called when the window is closed."""
  24. my_msg.set("{quit}")
  25. send()
  26. def start_messenger(event=None):
  27. messengerstart_button.pack_forget()
  28. background_label.pack_forget()
  29. scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
  30. msg_list.pack(side=tkinter.LEFT)
  31. msg_list.pack()
  32. messages_frame.pack()
  33. entry_field.pack(fill=tkinter.X)
  34. send_button.pack(fill=tkinter.X)
  35. top = tkinter.Tk()
  36. top.title("Brieftaube")
  37. #top.attributes('-fullscreen', True)
  38. top["bg"]="red3"
  39. #canvas = tkinter.Canvas(top,
  40. #width=300,
  41. #height=300)
  42. #canvas.pack(side=tkinter.BOTTOM, fill=tkinter.BOTH)
  43. img = tkinter.PhotoImage(file="Smiley.ppm")
  44. #canvas.create_image(200,200, image=img)
  45. background_label = tkinter.Label(top, image=img)
  46. background_label.place(x=0, y=0, relwidth=1, relheight=1)
  47. #background_label["bg"] = "black"
  48. background_label.pack()
  49. messengerstart_button = tkinter.Button(top, text="Start", command=start_messenger, activebackground="red3", bg="black", fg="red3", font="C059")
  50. messengerstart_button.pack(fill=tkinter.X)
  51. messages_frame = tkinter.Frame(top)
  52. my_msg = tkinter.StringVar() # For the messages to be sent.
  53. my_msg.set("")
  54. scrollbar = tkinter.Scrollbar(messages_frame, activebackground="red3", bg="black") # To navigate through past messages.
  55. # Following will contain the messages.
  56. msg_list = tkinter.Listbox(messages_frame, background="black", fg="red3", height=20, width=50, yscrollcommand=scrollbar.set)
  57. #canvas.pack()
  58. entry_field = tkinter.Entry(top, textvariable=my_msg, bg="black", fg="red3")
  59. entry_field.bind("<Return>", send)
  60. send_button = tkinter.Button(top, text="SEND", command=send, activebackground="red3", bg="black", fg="red3", font="C059")
  61. top.protocol("WM_DELETE_WINDOW", on_closing)
  62. #----Now comes the sockets part----
  63. HOST = input('Enter host: ')
  64. PORT = input('Enter port: ')
  65. if not PORT:
  66. PORT = 33000
  67. else:
  68. PORT = int(PORT)
  69. BUFSIZ = 1024
  70. ADDR = (HOST, PORT)
  71. client_socket = socket(AF_INET, SOCK_STREAM)
  72. client_socket.connect(ADDR)
  73. receive_thread = Thread(target=receive)
  74. receive_thread.start()
  75. tkinter.mainloop() # Starts GUI execution.