-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (53 loc) · 2.15 KB
/
Copy pathmain.py
File metadata and controls
72 lines (53 loc) · 2.15 KB
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
import threading
from tkinter import colorchooser
from gui import ChatWindow, ColorData
from network import NetworkHandler, encode_packet
class InputHandler:
net_handler: NetworkHandler
chat_window: ChatWindow
connected: bool
naming: bool
name: str
name_color: str
def __init__(self, net_handler, chat_window):
self.net_handler = net_handler
self.chat_window = chat_window
self.chat_window.input_box.bind("<Return>", self.handle_input)
self.chat_window.select_color.config(command=self.set_name_color)
self.name_color = self.chat_window.active_fg
self.connected = False
self.naming = True
def handle_input(self, _):
msg = self.chat_window.get_entry()
self.chat_window.clear_entry()
if self.naming:
self.name = msg
self.naming = False
self.chat_window.log_msg(
"Type 'host' to host a chat, or type in the ip address of a host you would like to join.")
elif self.connected:
msg = "<" + self.name + ">" + " " + msg
self.net_handler.broadcast(encode_packet(msg, [ColorData(1, len(self.name)+1, self.name_color)]))
if self.net_handler.hosting:
self.chat_window.log_msg(msg, [ColorData(1, len(self.name)+1, self.name_color)])
else:
try:
if msg == "host":
host_thread = threading.Thread(target=self.net_handler.host_chat, daemon=True)
host_thread.start()
else:
self.net_handler.connect(msg)
self.connected = True
except Exception as e:
print(e)
def set_name_color(self):
self.name_color = colorchooser.askcolor()[1]
def main():
chat_window = ChatWindow()
net_handler = NetworkHandler(chat_window)
input_handler = InputHandler(net_handler, chat_window)
net_handler.input_handler = input_handler
chat_window.log_msg("Enter a name to go by.")
chat_window.window.mainloop()
if __name__ == "__main__":
main()