forked from saavedra29/pychat-UDP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
236 lines (188 loc) · 9.32 KB
/
gui.py
File metadata and controls
236 lines (188 loc) · 9.32 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import tkinter as tk
import tkinter.ttk as ttk
from os import system, getpid
import platform
# This customization of the Text widget allows elements of the text to be colored.
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start="1.0", end="end", regexp=False):
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd", "searchLimit", count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
# tkinter code
class MainWindow(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.minsize(width=400, height=300)
self.geometry("580x400")
self.title("JUST GUI")
self.protocol('WM_DELETE_WINDOW', self.on_exit)
self.keyFromWindowInput = ''
# Images for the encryption indicator at the right of the input widget
self.imageOrangePath = 'images/orange15.png'
self.imageGreenPath = 'images/green15.png'
self.orangeImg = tk.PhotoImage(file=self.imageOrangePath)
self.greenImg = tk.PhotoImage(file=self.imageGreenPath)
# Create a menubar and associate it with the window
self.menubar = tk.Menu()
self.config(menu=self.menubar)
# Create File menu and add it to the menubar
self.file_menu = tk.Menu(self.menubar, tearoff=False)
self.menubar.add_cascade(label="File", menu=self.file_menu)
self.file_menu.add_command(label="Exit", command=self.on_exit)
# Server menu
self.server_menu = tk.Menu(self.menubar, tearoff=False)
self.menubar.add_cascade(label="Server", menu=self.server_menu)
self.server_menu.add_command(label="Start server", command=self.on_start_server)
self.server_menu.add_command(label="Stop server", command=self.on_stop_server)
# Encryption menu
self.gen_key_menu = tk.Menu(self.menubar, tearoff=False)
self.menubar.add_cascade(label="Encryption", menu=self.gen_key_menu)
self.gen_key_menu.add_command(label="Encryption on", command=self.on_encryption_on)
self.gen_key_menu.add_command(label="Encryption off", command=self.on_encryption_off)
self.gen_key_menu.add_command(label="Generate key", command=self.on_generate_key)
self.gen_key_menu.add_command(label="Place remote key", command=self.on_place_key)
# Create Connection frame
self.connection_frame = tk.Frame(self, borderwidth=5)
self.connection_frame.pack(side="top", fill="x")
# Create Peer combobox and add it to Connection frame
self.ipEntry = ttk.Entry(self.connection_frame)
self.ipEntry.pack(side="left")
# Create setPeer button and add it to Connection frame
self.setPeer_button = ttk.Button(self.connection_frame, text="Set peer",
command=self.on_setPeer)
self.setPeer_button.pack(side="left", padx=5)
# Create Connect button and add it to Connection frame
self.connect_button = ttk.Button(self.connection_frame, text="Connect",
command=self.on_connect)
self.connect_button.pack(side="left", padx=5)
# Create Disconnect button and add it to Connection frame
self.disconnect_button = ttk.Button(self.connection_frame, text="Disconnect",
command=self.on_disconnect)
self.disconnect_button.pack(side="left", padx=5)
# Create Status label
self.status_label = ttk.Label(self.connection_frame, text="Disconnected", width=17,
foreground="red",
relief='ridge', borderwidth=3, justify='center',
anchor='center')
self.status_label.pack(side="right", padx=5, ipadx=3, ipady=3)
# Create Output frame
self.output_frame = tk.Frame(self, borderwidth=5)
self.output_frame.pack(side="top", padx=0, fill="both", expand=True)
# Create Scrollbar and add it to the Output frame. This has to be packed before the Text widget
self.scrollbar = tk.Scrollbar(self.output_frame)
self.scrollbar.pack(side="right", fill="y", padx=5)
# Create Text widget and add it to the Output frame
self.output_text = CustomText(self.output_frame, height=10, width=40,
yscrollcommand=self.scrollbar.set)
self.output_text.tag_configure("green", foreground="green")
self.output_text.tag_configure("red", foreground="red")
self.output_text.pack(side="left", fill="both", expand=True)
# Create Input frame
self.input_frame = tk.Frame(self, borderwidth=5)
self.input_frame.pack(side="top", fill="x")
# Create User input entry and add it to Input frame
self.userInputEntry = tk.Entry(self.input_frame)
self.userInputEntry.bind("<Return>",
self.on_enter_press) # <Return> key binding for sending the data
self.userInputEntry.pack(side="left", fill="x", expand=True)
self.encryption_indicator = tk.Label(self.input_frame, image=self.orangeImg)
self.encryption_indicator.pack(side='right')
# Create Debug frame
self.debug_frame = tk.Frame(self)
self.debug_frame.pack(side="top", fill="x")
# Create Separator and add it to Debug frame
self.sep = ttk.Separator(self.debug_frame)
self.sep.pack(fill="x", pady=3)
self.debug_label = tk.Label(self.debug_frame)
self.debug_label.pack(side="left")
# Functions of the menu buttons
def insert_text(self, txt):
self.output_text.insert("end", txt + "\n")
self.output_text.highlight_pattern('Server ==>', 'red')
self.output_text.highlight_pattern('Peer ==>', 'green')
self.output_text.see("end")
def on_start_server(self):
self.insert_text("TODO: Start server")
def on_stop_server(self):
self.insert_text("TODO: Stop server")
# Exit function
def on_exit(self):
id = getpid()
os = platform.system()
if os == 'Linux':
command = 'kill ' + str(id)
system(command)
else:
command = 'taskkill -f /pid ' + str(id)
system(command)
# Set peer function
def on_setPeer(self):
print("set peer pressed")
# Connect button function
def on_connect(self):
print("connect button pressed")
# Disconnect button function
def on_disconnect(self):
print("disconnect button pressed")
# Send_data function
def on_enter_press(self, event):
print("enter key pressed")
def on_encryption_on(self):
pass
def on_encryption_off(self):
pass
def on_generate_key(self):
pass
def on_place_key(self):
pass
def on_create_key_window(self, key):
create_key_window = tk.Toplevel()
create_key_window.minsize(360, 200)
create_key_window.grab_set()
create_key_window.title('Key Generation')
instructions_message = tk.Message(create_key_window,
text='This is the automatically generated key. '
'The remote peer should have exactly the '
'same for the encryption-decryption to be '
'possible.\nUse Ctrl-C to copy.')
instructions_message.bind("<Configure>", lambda e:
instructions_message.configure(width=e.width - 10))
instructions_message.pack(fill=tk.X, expand=True)
messageEntry = tk.Entry(create_key_window, state='readonly')
var = tk.StringVar()
print(key)
print(key.decode('utf8'))
var.set(key.decode('utf8'))
messageEntry.config(textvariable=var)
messageEntry.pack(expand=True, fill=tk.X)
generate_key_button = tk.Button(create_key_window, text='OK',
command=create_key_window.destroy)
generate_key_button.pack()
def assign(self):
pass
def on_place_key_window(self):
self.place_key_window = tk.Toplevel()
self.place_key_window.minsize(360, 200)
self.place_key_window.grab_set()
self.place_key_window.title('Key Generation')
self.instructions_message = tk.Message(self.place_key_window,
text='Please place the remote peer key.')
self.instructions_message.bind('<Configure>', lambda e:
self.instructions_message.configure(width=e.width - 10))
self.instructions_message.pack(fill=tk.X, expand=True)
self.keyEntry = tk.Entry(self.place_key_window)
self.keyEntry.pack()
self.submit_key_button = tk.Button(self.place_key_window, text='Submit',
command = self.assign)
self.submit_key_button.pack()