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

#!/usr/bin/env python3
"""Script for Tkinter GUI chat client."""
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import tkinter
def receive():
"""Handles receiving of messages."""
while True:
try:
msg = client_socket.recv(BUFSIZ).decode("utf8")
msg_list.insert(tkinter.END, msg)
except OSError: # Possibly client has left the chat.
break
def send(event=None): # event is passed by binders.
"""Handles sending of messages."""
msg = my_msg.get()
my_msg.set("") # Clears input field.
client_socket.send(bytes(msg, "utf8"))
if msg == "{quit}":
client_socket.close()
top.quit()
def on_closing(event=None):
"""This function is to be called when the window is closed."""
my_msg.set("{quit}")
send()
def start_messenger(event=None):
messenger_var = True
top = tkinter.Tk()
top.title("Brieftaube")
#top.attributes('-fullscreen', True)
top["bg"]="red3"
#canvas = tkinter.Canvas(top,
#width=300,
#height=300)
#canvas.pack(side=tkinter.BOTTOM, fill=tkinter.BOTH)
#img = tkinter.PhotoImage(file="Smiley.ppm")
##canvas.create_image(200,200, image=img)
#background_label = tkinter.Label(top, image=img)
#background_label.place(x=0, y=0, relwidth=1, relheight=1)
##background_label["bg"] = "black"
#background_label.pack()
#messengerstart_button = tkinter.Button(top, text="Start", command=start_messenger, activebackground="red3", bg="black", fg="red3", font="C059")
#messengerstart_button.pack(fill=tkinter.X)
#messengerstart_button.pack_forget()
#background_label.pack_forget()
messages_frame = tkinter.Frame(top)
my_msg = tkinter.StringVar() # For the messages to be sent.
my_msg.set("")
scrollbar = tkinter.Scrollbar(messages_frame, activebackground="red3", bg="black") # To navigate through past messages.
# Following will contain the messages.
msg_list = tkinter.Listbox(messages_frame, background="black", fg="red3", height=20, width=50, yscrollcommand=scrollbar.set)
#canvas.pack()
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT)
msg_list.pack()
messages_frame.pack()
entry_field = tkinter.Entry(top, textvariable=my_msg, bg="black", fg="red3")
entry_field.bind("<Return>", send)
entry_field.pack(fill=tkinter.X)
send_button = tkinter.Button(top, text="SEND", command=send, activebackground="red3", bg="black", fg="red3", font="C059")
send_button.pack(fill=tkinter.X)
top.protocol("WM_DELETE_WINDOW", on_closing)
#----Now comes the sockets part----
HOST = input('Enter host: ')
PORT = input('Enter port: ')
if not PORT:
PORT = 33000
else:
PORT = int(PORT)
BUFSIZ = 1024
ADDR = (HOST, PORT)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
receive_thread = Thread(target=receive)
receive_thread.start()
tkinter.mainloop() # Starts GUI execution.