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.

99 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. messenger_var = True
  28. top = tkinter.Tk()
  29. top.title("Brieftaube")
  30. #top.attributes('-fullscreen', True)
  31. top["bg"]="red3"
  32. #canvas = tkinter.Canvas(top,
  33. #width=300,
  34. #height=300)
  35. #canvas.pack(side=tkinter.BOTTOM, fill=tkinter.BOTH)
  36. #img = tkinter.PhotoImage(file="Smiley.ppm")
  37. ##canvas.create_image(200,200, image=img)
  38. #background_label = tkinter.Label(top, image=img)
  39. #background_label.place(x=0, y=0, relwidth=1, relheight=1)
  40. ##background_label["bg"] = "black"
  41. #background_label.pack()
  42. #messengerstart_button = tkinter.Button(top, text="Start", command=start_messenger, activebackground="red3", bg="black", fg="red3", font="C059")
  43. #messengerstart_button.pack(fill=tkinter.X)
  44. #messengerstart_button.pack_forget()
  45. #background_label.pack_forget()
  46. messages_frame = tkinter.Frame(top)
  47. my_msg = tkinter.StringVar() # For the messages to be sent.
  48. my_msg.set("")
  49. scrollbar = tkinter.Scrollbar(messages_frame, activebackground="red3", bg="black") # To navigate through past messages.
  50. # Following will contain the messages.
  51. msg_list = tkinter.Listbox(messages_frame, background="black", fg="red3", height=20, width=50, yscrollcommand=scrollbar.set)
  52. #canvas.pack()
  53. scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
  54. msg_list.pack(side=tkinter.LEFT)
  55. msg_list.pack()
  56. messages_frame.pack()
  57. entry_field = tkinter.Entry(top, textvariable=my_msg, bg="black", fg="red3")
  58. entry_field.bind("<Return>", send)
  59. entry_field.pack(fill=tkinter.X)
  60. send_button = tkinter.Button(top, text="SEND", command=send, activebackground="red3", bg="black", fg="red3", font="C059")
  61. send_button.pack(fill=tkinter.X)
  62. top.protocol("WM_DELETE_WINDOW", on_closing)
  63. #----Now comes the sockets part----
  64. HOST = input('Enter host: ')
  65. PORT = input('Enter port: ')
  66. if not PORT:
  67. PORT = 33000
  68. else:
  69. PORT = int(PORT)
  70. BUFSIZ = 1024
  71. ADDR = (HOST, PORT)
  72. client_socket = socket(AF_INET, SOCK_STREAM)
  73. client_socket.connect(ADDR)
  74. receive_thread = Thread(target=receive)
  75. receive_thread.start()
  76. tkinter.mainloop() # Starts GUI execution.