2019-07-22 15:09:11 +02:00
|
|
|
from tkinter import Tk, Frame, Scrollbar, Label, Text, Button, Entry, StringVar, IntVar, TclError
|
|
|
|
from tkinter.messagebox import showerror, showwarning
|
2019-07-18 18:20:25 +02:00
|
|
|
from client import Client
|
|
|
|
from threading import Thread
|
2019-07-22 15:09:11 +02:00
|
|
|
from socket import error as socket_error
|
|
|
|
|
|
|
|
destroy = False
|
2019-07-18 18:20:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def on_closing():
|
2019-07-22 15:09:11 +02:00
|
|
|
global destroy
|
|
|
|
destroy = True
|
2019-07-18 18:20:25 +02:00
|
|
|
try:
|
2019-07-22 15:09:11 +02:00
|
|
|
client.send_server("quit")
|
|
|
|
except TclError:
|
2019-07-18 18:20:25 +02:00
|
|
|
pass
|
2019-07-22 15:09:11 +02:00
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
tchat.destroy()
|
|
|
|
except TclError:
|
|
|
|
pass
|
2019-07-18 18:20:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def start():
|
|
|
|
if host.get() and port.get():
|
|
|
|
try:
|
|
|
|
global client
|
|
|
|
client = Client(host.get(), port.get())
|
2019-07-22 15:09:11 +02:00
|
|
|
except (socket_error, ConnectionError):
|
|
|
|
showerror("Error", "Can't connect to server !")
|
2019-07-18 18:20:25 +02:00
|
|
|
else:
|
|
|
|
login.destroy()
|
|
|
|
|
|
|
|
|
|
|
|
def receive():
|
|
|
|
while True:
|
2019-07-22 15:09:11 +02:00
|
|
|
try:
|
|
|
|
msg = client.receive_server()
|
|
|
|
if msg.lower() == "quit" or not msg:
|
|
|
|
raise ConnectionError("Client quit")
|
|
|
|
except (socket_error, ConnectionError, AttributeError):
|
|
|
|
show_message("""}------------------------------{
|
|
|
|
/!\\ [Receive system offline] /!\\
|
|
|
|
Press Enter to exit
|
|
|
|
}------------------------------{""")
|
2019-07-18 18:20:25 +02:00
|
|
|
break
|
2019-07-22 15:09:11 +02:00
|
|
|
else:
|
|
|
|
show_message(msg)
|
2019-07-18 18:20:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def send(event=None):
|
2019-07-22 15:09:11 +02:00
|
|
|
try:
|
|
|
|
client.send_server(message.get())
|
|
|
|
if not receive_thread.is_alive() or message.get().lower() == "quit":
|
|
|
|
raise ConnectionError("Client quit")
|
|
|
|
except (socket_error, ConnectionError):
|
|
|
|
showwarning("Disconnected", "Disconnected from server")
|
|
|
|
on_closing()
|
|
|
|
else:
|
|
|
|
message.set("")
|
|
|
|
|
|
|
|
|
|
|
|
def show_message(msg):
|
|
|
|
if msg[-1:] != "\n":
|
|
|
|
msg += "\n"
|
|
|
|
if not destroy:
|
|
|
|
chat_message.configure(state="normal")
|
|
|
|
chat_message.insert("end", msg)
|
|
|
|
chat_message.configure(state="disable")
|
2019-07-18 18:20:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
login = Tk()
|
|
|
|
login.title("Login")
|
|
|
|
host = StringVar()
|
|
|
|
port = IntVar()
|
|
|
|
Label(login, text="Host & port:").pack()
|
|
|
|
login_f = Frame(login)
|
|
|
|
login_f.pack()
|
|
|
|
Entry(login_f, textvariable=host, width=14).grid(row=0, column=0)
|
|
|
|
Entry(login_f, textvariable=port, width=4).grid(row=0, column=1)
|
|
|
|
Button(login, text="Submit", command=start).pack()
|
|
|
|
login.mainloop()
|
|
|
|
|
|
|
|
tchat = Tk()
|
|
|
|
tchat.title("PyTchat")
|
|
|
|
tchat.protocol("WM_DELETE_WINDOW", on_closing)
|
|
|
|
chat = Frame(tchat)
|
|
|
|
chat.pack()
|
|
|
|
scrollbar = Scrollbar(chat)
|
2019-07-19 15:47:45 +02:00
|
|
|
scrollbar.pack(side="right", fill="y")
|
|
|
|
chat_message = Text(chat, height=15, width=50, yscrollcommand=scrollbar.set, state="disable")
|
|
|
|
chat_message.pack(side="left", fill="both")
|
2019-07-22 15:09:11 +02:00
|
|
|
receive_thread = Thread(target=receive)
|
|
|
|
receive_thread.start()
|
2019-07-18 18:20:25 +02:00
|
|
|
|
|
|
|
entry = Frame(tchat)
|
|
|
|
entry.pack()
|
|
|
|
message = StringVar()
|
|
|
|
field = Entry(entry, textvariable=message)
|
|
|
|
field.bind("<Return>", send)
|
|
|
|
field.grid(row=0, column=0)
|
|
|
|
Button(entry, text="Send", command=send).grid(row=0, column=1)
|
|
|
|
tchat.mainloop()
|