-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
106 lines (100 loc) · 4.09 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
@author: Fotios Lygerakis
@UTA ID: 1001774373
"""
import socket
import threading
import PySimpleGUI as sg
from client import Client
from utils.utils_gui import server_layout, client_layout
from server import Server
"""
Code based on https://pysimplegui.readthedocs.io/en/latest/cookbook/
"""
class GUI:
def __init__(self, name):
"""
Init method
:param name: the name of the user (server or client)
"""
self.server, self.client = None, None
# helper variables
if name == "Client":
self.is_client = True
self.is_server = False
else:
self.is_client = False
self.is_server = True
# chose the appropriate layout for the server and the client
if self.is_server:
self.layout = server_layout
self.server = Server()
elif self.is_client:
self.layout = client_layout
self.client = Client()
self.logged_in = False
# create a window
self.window = sg.Window(name, self.layout)
def run(self):
"""
Function for handling the GUI
"""
while True:
# read the events on the gui and their values
event, values = self.window.read()
# exit gui when window is closed or Exit button is pressed
if event == sg.WIN_CLOSED or event == 'Exit':
# close up sockets
if self.is_server:
try:
self.server.socket.shutdown(socket.SHUT_RD)
self.server.socket.close()
except:
print("server shut down")
break
else:
self.client.socket.shutdown(socket.SHUT_RD)
break
# Client's gui
if self.is_client:
# on submit button take the username that has been entered
if event == 'Login' and not self.logged_in:
# try to log in with the given username
print("Trying to log in: {}".format(values[0]))
self.logged_in = self.client.set_up_connection(values[0])
# not logged in
if not self.logged_in:
print("Could not login")
print("Username already taken.\nPlease use another one.")
else:
print('You are logged in!')
# start the main thread of the client as a thread
thread = threading.Thread(target=self.client.main)
thread.start()
# if logged in
if self.logged_in:
# "Send Text" is pressed
if event == "Send Text":
# changes the clients variable, so that to turn on the file exchange with the server
self.client.send_file_to_server = True
# pass on the filename given by the gui to the client thread
self.client.filename = values[1]
# "Add" button is pressed
elif event == "Add":
# add entry from gui to the client's thread queue
self.client.add_to_queue(values[2])
# Server's gui
elif self.is_server:
# start a thread for the server when the Go button is pressed
if event == 'Go':
# deactivate Go button
self.window.FindElement('Go').Update(disabled=True)
thread = threading.Thread(target=self.server.main)
thread.start()
# when "Client List" button is pressed the online usernames are being printed on the gui
elif event == 'Client List':
print("Client List Online: {}".format(self.server.get_live_usernames()))
# otherwise print the values in the gui
else:
print(values)
self.window.close()